| 1 |
|
| 2 |
#define _GNU_SOURCE |
| 3 |
#include <sys/types.h> |
| 4 |
#include <sys/stat.h> |
| 5 |
#include <fcntl.h> |
| 6 |
#include <getopt.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <stdint.h> |
| 10 |
#include <string.h> |
| 11 |
#include <stdbool.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include <endian.h> |
| 14 |
#include <errno.h> |
| 15 |
#include <limits.h> |
| 16 |
|
| 17 |
#define NOSIZE ((size_t)-1) |
| 18 |
|
| 19 |
extern uint32_t crc32(uint32_t iv, uint32_t sv, const void *data, size_t n); |
| 20 |
extern uint32_t crc32_final(uint32_t iv); |
| 21 |
|
| 22 |
|
| 23 |
struct __attribute__((__packed__)) ubootsplhdr { |
| 24 |
uint32_t sofs; |
| 25 |
uint32_t bofs; |
| 26 |
uint8_t zro2[636]; |
| 27 |
uint32_t vers; |
| 28 |
|
| 29 |
uint32_t fsiz; |
| 30 |
uint32_t res1; |
| 31 |
uint32_t crcs; |
| 32 |
uint8_t zro3[364]; |
| 33 |
}; |
| 34 |
|
| 35 |
struct hdr_conf_t { |
| 36 |
const char name[PATH_MAX]; |
| 37 |
uint32_t vers; |
| 38 |
uint32_t bofs; |
| 39 |
bool creat_hdr_flag; |
| 40 |
bool fixed_img_hdr_flag; |
| 41 |
}; |
| 42 |
|
| 43 |
static struct ubootsplhdr ubsplhdr; |
| 44 |
static struct ubootsplhdr imghdr; |
| 45 |
static struct hdr_conf_t g_hdr_conf; |
| 46 |
|
| 47 |
static char ubootspl[181072-sizeof(struct ubootsplhdr)+1]; |
| 48 |
static char outpath[PATH_MAX]; |
| 49 |
|
| 50 |
#define DEFVERSID 0x01010101 |
| 51 |
#define DEFBACKUP 0x200000U |
| 52 |
#define CRCFAILED 0x5A5A5A5A |
| 53 |
|
| 54 |
static void xerror(int errnoval, const char *s) |
| 55 |
{ |
| 56 |
if (errnoval) perror(s); |
| 57 |
else fprintf(stderr, "%s\n", s); |
| 58 |
exit(2); |
| 59 |
} |
| 60 |
|
| 61 |
static void usage(void) |
| 62 |
{ |
| 63 |
const char help[] = { |
| 64 |
"\n StarFive spl tool\n\n" |
| 65 |
"usage:\n" |
| 66 |
"-c, --creat-splhdr creat spl hdr\n" |
| 67 |
"-i, --fix-imghdr fixed img hdr for emmc boot.\n" |
| 68 |
"-a, --spl-bak-addr set backup SPL addr(default: 0x200000)\n" |
| 69 |
"-v, --version set version (default: 0x01010101)\n" |
| 70 |
"-f, --file input file name(spl/img)\n" |
| 71 |
"-h, --help show this information\n" |
| 72 |
}; |
| 73 |
puts(help); |
| 74 |
} |
| 75 |
|
| 76 |
static int parse_args(int argc, char **argv) |
| 77 |
{ |
| 78 |
uint32_t v; |
| 79 |
|
| 80 |
enum { |
| 81 |
OPTION_CREAD_HDR = 1, |
| 82 |
OPTION_FIXED_HDR, |
| 83 |
OPTION_SBL_BAK_OFFSET, |
| 84 |
OPTION_VERSION, |
| 85 |
OPTION_FILENAME, |
| 86 |
OPTION_HELP, |
| 87 |
}; |
| 88 |
|
| 89 |
static struct option long_options[] = |
| 90 |
{ |
| 91 |
{"creat-splhdr" , no_argument, NULL, OPTION_CREAD_HDR}, |
| 92 |
{"fix-imghdr" , no_argument, NULL, OPTION_FIXED_HDR}, |
| 93 |
{"spl-bak-addr" , required_argument, NULL, OPTION_SBL_BAK_OFFSET}, |
| 94 |
{"version", required_argument, NULL, OPTION_VERSION}, |
| 95 |
{"file", required_argument, NULL, OPTION_FILENAME}, |
| 96 |
{"help", no_argument, NULL, OPTION_HELP}, |
| 97 |
{0, 0, 0, 0} |
| 98 |
}; |
| 99 |
|
| 100 |
while (1) |
| 101 |
{ |
| 102 |
|
| 103 |
int option_index = 0; |
| 104 |
|
| 105 |
int c = getopt_long(argc, argv, "cio:v:f:h", long_options, &option_index); |
| 106 |
|
| 107 |
|
| 108 |
if (c == -1) |
| 109 |
break; |
| 110 |
|
| 111 |
switch (c) { |
| 112 |
case 0: |
| 113 |
|
| 114 |
if (long_options[option_index].flag != 0) |
| 115 |
break; |
| 116 |
|
| 117 |
case 'c': |
| 118 |
case OPTION_CREAD_HDR: |
| 119 |
g_hdr_conf.creat_hdr_flag = true; |
| 120 |
g_hdr_conf.fixed_img_hdr_flag = false; |
| 121 |
break; |
| 122 |
|
| 123 |
case 'i': |
| 124 |
case OPTION_FIXED_HDR: |
| 125 |
g_hdr_conf.fixed_img_hdr_flag = true; |
| 126 |
g_hdr_conf.creat_hdr_flag = false; |
| 127 |
break; |
| 128 |
|
| 129 |
case 'a': |
| 130 |
case OPTION_SBL_BAK_OFFSET: |
| 131 |
v = (uint32_t)strtoul(optarg, NULL, 16); |
| 132 |
v = htole32(v); |
| 133 |
g_hdr_conf.bofs = v; |
| 134 |
break; |
| 135 |
|
| 136 |
case 'v': |
| 137 |
case OPTION_VERSION: |
| 138 |
v = (uint32_t)strtoul(optarg, NULL, 16); |
| 139 |
v = htole32(v); |
| 140 |
g_hdr_conf.vers = v; |
| 141 |
break; |
| 142 |
|
| 143 |
case 'f': |
| 144 |
case OPTION_FILENAME: |
| 145 |
strcpy((char*)g_hdr_conf.name, optarg); |
| 146 |
break; |
| 147 |
|
| 148 |
case 'h': |
| 149 |
case OPTION_HELP: |
| 150 |
usage(); |
| 151 |
break; |
| 152 |
|
| 153 |
default: |
| 154 |
usage(); |
| 155 |
break; |
| 156 |
} |
| 157 |
} |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
int spl_creat_hdr(struct hdr_conf_t *conf) |
| 162 |
{ |
| 163 |
int fd; |
| 164 |
uint32_t v; |
| 165 |
size_t sz; |
| 166 |
|
| 167 |
if (!conf->creat_hdr_flag) |
| 168 |
return 0; |
| 169 |
|
| 170 |
ubsplhdr.sofs = htole32(0x240U); |
| 171 |
ubsplhdr.res1 = htole32(0x400U); |
| 172 |
ubsplhdr.bofs = conf->bofs ? conf->bofs : htole32(DEFBACKUP); |
| 173 |
ubsplhdr.vers = conf->vers ? conf->vers : htole32(DEFVERSID); |
| 174 |
|
| 175 |
printf("ubsplhdr.sofs:%#x, ubsplhdr.bofs:%#x, ubsplhdr.vers:%#x name:%s\n", |
| 176 |
ubsplhdr.sofs, ubsplhdr.bofs, ubsplhdr.vers, conf->name); |
| 177 |
|
| 178 |
fd = open(conf->name, O_RDONLY); |
| 179 |
if (fd == -1) xerror(errno, conf->name); |
| 180 |
|
| 181 |
sz = (size_t)read(fd, ubootspl, sizeof(ubootspl)); |
| 182 |
if (sz == NOSIZE) xerror(errno, conf->name); |
| 183 |
if (sz >= (sizeof(ubootspl))) |
| 184 |
xerror(0, "File too large! Please rebuild your SPL with -Os. Maximum allowed size is 180048 bytes."); |
| 185 |
v = htole32((uint32_t)sz); |
| 186 |
ubsplhdr.fsiz = v; |
| 187 |
|
| 188 |
close(fd); |
| 189 |
snprintf(outpath, sizeof(outpath), "%s.normal.out", conf->name); |
| 190 |
fd = creat(outpath, 0666); |
| 191 |
if (fd == -1) xerror(errno, outpath); |
| 192 |
|
| 193 |
v = crc32(~0U, 0x04c11db7U, ubootspl, sz); |
| 194 |
v = crc32_final(v); |
| 195 |
v = htole32(v); |
| 196 |
ubsplhdr.crcs = v; |
| 197 |
|
| 198 |
write(fd, &ubsplhdr, sizeof(struct ubootsplhdr)); |
| 199 |
write(fd, ubootspl, sz); |
| 200 |
|
| 201 |
close(fd); |
| 202 |
|
| 203 |
printf("SPL written to %s successfully.\n", outpath); |
| 204 |
|
| 205 |
return 0; |
| 206 |
} |
| 207 |
|
| 208 |
int img_fixed_hdr(struct hdr_conf_t *conf) |
| 209 |
{ |
| 210 |
int fd; |
| 211 |
size_t sz; |
| 212 |
|
| 213 |
if (!conf->fixed_img_hdr_flag) |
| 214 |
return 0; |
| 215 |
|
| 216 |
fd = open(conf->name, O_RDWR); |
| 217 |
if (fd == -1) xerror(errno, conf->name); |
| 218 |
|
| 219 |
sz = (size_t)read(fd, &imghdr, sizeof(imghdr)); |
| 220 |
if (sz == NOSIZE) xerror(errno, conf->name); |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
imghdr.bofs = conf->bofs ? conf->bofs : htole32(DEFBACKUP); |
| 228 |
imghdr.crcs = htole32(CRCFAILED); |
| 229 |
|
| 230 |
lseek(fd, 0x0, SEEK_SET); |
| 231 |
write(fd, &imghdr, sizeof(imghdr)); |
| 232 |
close(fd); |
| 233 |
|
| 234 |
printf("IMG %s fixed hdr successfully.\n", conf->name); |
| 235 |
|
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
int main(int argc, char **argv) |
| 240 |
{ |
| 241 |
parse_args(argc, argv); |
| 242 |
spl_creat_hdr(&g_hdr_conf); |
| 243 |
img_fixed_hdr(&g_hdr_conf); |
| 244 |
} |