1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Assemble an OpenWrt image for the legacy Inteno/Genexis Y3 updater. 4 * 5 * Version 8 consists of a 1024-byte, NUL-padded text header, payloads and 6 * a 32-byte lowercase ASCII MD5 of everything before the checksum. 7 * Offsets are absolute file offsets; sizes and offsets are decimal. 8 * 9 * This utility wraps an already assembled UBI image (see ubinize). It 10 * deliberately emits no CFE ROM or rescue payload. The singlepureubi CFE 11 * updater replaces the UBI area while keeping the existing loader code. 12 * That updater can still rewrite NVRAM in the loader partition: cfe 0 13 * does NOT guarantee that the entire loader partition remains untouched. 14 * 15 * On XG6846B, the UBI image must retain the original cferam.000 in the 16 * kernel_0 boot bundle, alongside vmlinux.lz and 96846.dtb. The metadata 17 * volumes and boot bundle use Broadcom formats, not ordinary filesystems. 18 * Kernel wrapping, boot metadata and UBI geometry belong to the board's 19 * image recipe. This tool neither reformats nor edits the supplied UBI. 20 */ 21 22 #define _POSIX_C_SOURCE 200809L 23 #define _FILE_OFFSET_BITS 64 24 25 #include <errno.h> 26 #include <inttypes.h> 27 #include <limits.h> 28 #include <stdint.h> 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include <sys/stat.h> 33 #include <unistd.h> 34 35 #include "md5.h" 36 37 #define Y3_HEADER_SIZE 1024 38 /* The legacy CFE tag scanner stops short of the end of the header. */ 39 #define Y3_HEADER_TEXT_MAX 1004 40 #define Y3_MD5_SIZE 32 41 #define Y3_DEFAULT_MAX_SIZE 0x03a20000 42 #define UBI_EC_HEADER_SIZE 64 43 44 struct y3_options { 45 const char *input; 46 const char *output; 47 const char *chip; 48 const char *model; 49 const char *arch; 50 const char *release; 51 const char *customer; 52 const char *revision; 53 uint32_t max_size; 54 }; 55 56 static void usage(FILE *stream, const char *prog) 57 { 58 fprintf(stream, 59 "Usage: %s -i <ubi> -o <y3> -c <chip> -m <model> -a <arch> [options]\n" 60 "\n" 61 " -i <file> Prepared UBI image containing kernel and rootfs\n" 62 " -o <file> Output Y3 image\n" 63 " -c <id> Chip ID as printed by CFE, e.g. 6846 or 63138\n" 64 " -m <string> Model identifier(s), e.g. PANTHER\n" 65 " -a <string> Architecture identifier(s), e.g. 'all arm_xscale'\n" 66 " -r <string> Release (default: OpenWrt)\n" 67 " -u <string> Customer identifier (default: OpenWrt)\n" 68 " -g <string> Source revision (default: unknown)\n" 69 " -s <bytes> Maximum total size, decimal or 0x-prefixed\n" 70 " (default: 0x03a20000, the XG6846B CFE limit)\n" 71 " -h Show this help\n" 72 "\n" 73 "Creates version 8 singlepureubi/bpartit32 images with no CFE ROM\n" 74 "or rescue payload. The UBI boot bundle must retain CFE RAM where\n" 75 "required. CFE may still update NVRAM. See docs/y3-image.md.\n", 76 prog); 77 } 78 79 static int valid_text(const char *name, const char *value) 80 { 81 const unsigned char *p = (const unsigned char *)value; 82 83 if (value && *value) { 84 for (; *p; p++) { 85 if (*p < ' ' || *p > '~') 86 break; 87 } 88 if (!*p) 89 return 1; 90 } 91 fprintf(stderr, "%s must be nonempty printable ASCII without newlines\n", 92 name); 93 return 0; 94 } 95 96 static int parse_size(const char *str, uint32_t *size) 97 { 98 char *end; 99 uintmax_t value; 100 101 errno = 0; 102 value = strtoumax(str, &end, !strncmp(str, "0x", 2) ? 16 : 10); 103 if (*str < '' || *str > '9' || *end || errno || 104 value < Y3_HEADER_SIZE + UBI_EC_HEADER_SIZE + Y3_MD5_SIZE || 105 value > INT32_MAX) { 106 fprintf(stderr, "Invalid maximum image size: %s\n", str); 107 return -1; 108 } 109 *size = value; 110 return 0; 111 } 112 113 static int make_header(char *header, const struct y3_options *opts, 114 uint32_t ubi_size) 115 { 116 uint32_t md5_offset = Y3_HEADER_SIZE + ubi_size; 117 int len; 118 119 memset(header, 0, Y3_HEADER_SIZE); 120 len = snprintf(header, Y3_HEADER_SIZE, 121 "IntenoIopY\n" 122 "version 8\n" 123 "integrity MD5SUM\n" 124 "git %s\n" 125 "chip %s\n" 126 "arch %s\n" 127 "model %s\n" 128 "release %s\n" 129 "customer %s\n" 130 "cfeofs %u\ncfe 0\n" 131 "rescueofs %u\nrescue 0\n" 132 "vmlinuxofs %u\nvmlinux 0\n" 133 "ubifsofs %u\nubifs 0\n" 134 "ubiofs %u\nubi %" PRIu32 "\n" 135 "pkginfoofs %" PRIu32 "\npkginfo 0\n" 136 "scrofs %" PRIu32 "\nscr 0\n" 137 "flags singlepureubi bpartit32\n" 138 "sigofs %" PRIu32 "\nsig 0\n" 139 "md5ofs %" PRIu32 "\nmd5 32\n" 140 "size %" PRIu32 "\n", 141 opts->revision, opts->chip, opts->arch, opts->model, 142 opts->release, opts->customer, 143 Y3_HEADER_SIZE, Y3_HEADER_SIZE, Y3_HEADER_SIZE, 144 Y3_HEADER_SIZE, Y3_HEADER_SIZE, ubi_size, 145 md5_offset, md5_offset, md5_offset, md5_offset, 146 md5_offset + Y3_MD5_SIZE); 147 if (len < 0 || len > Y3_HEADER_TEXT_MAX) { 148 fprintf(stderr, "Metadata does not fit in the Y3 header\n"); 149 return -1; 150 } 151 return 0; 152 } 153 154 static int write_image(const struct y3_options *opts) 155 { 156 unsigned char buf[65536], digest[16]; 157 char header[Y3_HEADER_SIZE], hex[Y3_MD5_SIZE + 1]; 158 struct stat input_stat, output_stat; 159 FILE *input = NULL, *output = NULL; 160 char *temporary = NULL; 161 uint32_t remaining; 162 MD5_CTX md5; 163 mode_t mask; 164 size_t count; 165 int fd, i, ret = -1; 166 167 input = fopen(opts->input, "rb"); 168 if (!input) { 169 fprintf(stderr, "%s: %s\n", opts->input, strerror(errno)); 170 goto out; 171 } 172 if (fstat(fileno(input), &input_stat) || 173 !S_ISREG(input_stat.st_mode)) { 174 fprintf(stderr, "%s: input must be a regular file\n", opts->input); 175 goto out; 176 } 177 if (input_stat.st_size < UBI_EC_HEADER_SIZE || 178 (uintmax_t)input_stat.st_size > 179 opts->max_size - Y3_HEADER_SIZE - Y3_MD5_SIZE) { 180 fprintf(stderr, "%s: invalid UBI size or image exceeds %" PRIu32 181 " bytes\n", opts->input, opts->max_size); 182 goto out; 183 } 184 if (!stat(opts->output, &output_stat) && 185 input_stat.st_dev == output_stat.st_dev && 186 input_stat.st_ino == output_stat.st_ino) { 187 fprintf(stderr, "Input and output refer to the same file\n"); 188 goto out; 189 } 190 if (fread(buf, 1, UBI_EC_HEADER_SIZE, input) != UBI_EC_HEADER_SIZE || 191 memcmp(buf, "UBI#", 4) || buf[4] != 1) { 192 fprintf(stderr, "%s: expected a UBI version 1 image\n", opts->input); 193 goto out; 194 } 195 if (fseek(input, 0, SEEK_SET)) { 196 fprintf(stderr, "%s: cannot rewind input\n", opts->input); 197 goto out; 198 } 199 remaining = input_stat.st_size; 200 if (make_header(header, opts, remaining)) 201 goto out; 202 203 /* Publish only a complete image; preserve any old output on failure. */ 204 temporary = malloc(strlen(opts->output) + sizeof(".XXXXXX")); 205 if (!temporary) { 206 fprintf(stderr, "Out of memory\n"); 207 goto out; 208 } 209 sprintf(temporary, "%s.XXXXXX", opts->output); 210 fd = mkstemp(temporary); 211 if (fd < 0) { 212 fprintf(stderr, "%s: %s\n", opts->output, strerror(errno)); 213 free(temporary); 214 temporary = NULL; 215 goto out; 216 } 217 mask = umask(0); 218 umask(mask); 219 if (fchmod(fd, 0666 & ~mask)) { 220 close(fd); 221 goto write_error; 222 } 223 output = fdopen(fd, "wb"); 224 if (!output) { 225 close(fd); 226 goto write_error; 227 } 228 MD5_Init(&md5); 229 MD5_Update(&md5, header, sizeof(header)); 230 if (fwrite(header, 1, sizeof(header), output) != sizeof(header)) 231 goto write_error; 232 while (remaining) { 233 count = remaining < sizeof(buf) ? remaining : sizeof(buf); 234 if (fread(buf, 1, count, input) != count) { 235 fprintf(stderr, "%s: short read or read error\n", opts->input); 236 goto out; 237 } 238 MD5_Update(&md5, buf, count); 239 if (fwrite(buf, 1, count, output) != count) 240 goto write_error; 241 remaining -= count; 242 } 243 if (fgetc(input) != EOF || ferror(input)) { 244 fprintf(stderr, "%s: input changed size or read failed\n", opts->input); 245 goto out; 246 } 247 MD5_Final(digest, &md5); 248 for (i = 0; i < 16; i++) 249 snprintf(hex + 2 * i, 3, "%02x", digest[i]); 250 if (fwrite(hex, 1, Y3_MD5_SIZE, output) != Y3_MD5_SIZE) 251 goto write_error; 252 if (fclose(output)) { 253 output = NULL; 254 goto write_error; 255 } 256 output = NULL; 257 if (rename(temporary, opts->output)) 258 goto write_error; 259 ret = 0; 260 goto out; 261 262 write_error: 263 fprintf(stderr, "%s: output failed: %s\n", opts->output, strerror(errno)); 264 out: 265 if (input) 266 fclose(input); 267 if (output) 268 fclose(output); 269 if (temporary) { 270 if (ret) 271 unlink(temporary); 272 free(temporary); 273 } 274 return ret; 275 } 276 277 int main(int argc, char **argv) 278 { 279 struct y3_options opts = { 280 .release = "OpenWrt", 281 .customer = "OpenWrt", 282 .revision = "unknown", 283 .max_size = Y3_DEFAULT_MAX_SIZE, 284 }; 285 int opt; 286 287 while ((opt = getopt(argc, argv, "i:o:c:m:a:r:u:g:s:h")) != -1) { 288 switch (opt) { 289 case 'i': 290 opts.input = optarg; 291 break; 292 case 'o': 293 opts.output = optarg; 294 break; 295 case 'c': 296 opts.chip = optarg; 297 break; 298 case 'm': 299 opts.model = optarg; 300 break; 301 case 'a': 302 opts.arch = optarg; 303 break; 304 case 'r': 305 opts.release = optarg; 306 break; 307 case 'u': 308 opts.customer = optarg; 309 break; 310 case 'g': 311 opts.revision = optarg; 312 break; 313 case 's': 314 if (parse_size(optarg, &opts.max_size)) 315 return EXIT_FAILURE; 316 break; 317 case 'h': 318 usage(stdout, argv[0]); 319 return EXIT_SUCCESS; 320 default: 321 usage(stderr, argv[0]); 322 return EXIT_FAILURE; 323 } 324 } 325 if (!opts.input || !opts.output || !opts.chip || !opts.model || 326 !opts.arch || optind != argc) { 327 usage(stderr, argv[0]); 328 return EXIT_FAILURE; 329 } 330 /* CFE parses the chip ID as hexadecimal, accepting digits 0..9 only. */ 331 if (!*opts.chip || strlen(opts.chip) > 8 || 332 strspn(opts.chip, "0123456789") != strlen(opts.chip)) { 333 fprintf(stderr, "Chip ID must contain one to eight digits\n"); 334 return EXIT_FAILURE; 335 } 336 if (!valid_text("model", opts.model) || !valid_text("arch", opts.arch) || 337 !valid_text("release", opts.release) || 338 !valid_text("customer", opts.customer) || 339 !valid_text("revision", opts.revision)) 340 return EXIT_FAILURE; 341 342 return write_image(&opts) ? EXIT_FAILURE : EXIT_SUCCESS; 343 } 344
This page was automatically generated by LXR 0.3.1. • OpenWrt