1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * zytrx - add header to images for ZyXEL NR7101 4 * 5 * Based on add_header.c - partially based on OpenWrt's 6 * motorola-bin.c 7 * 8 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org> 9 * Gabor Juhos <juhosg@openwrt.org> 10 * Copyright (C) 2021 Bjørn Mork <bjorn@mork.no> 11 */ 12 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <stddef.h> 16 #include <unistd.h> 17 #include <errno.h> 18 #include <fcntl.h> 19 #include <sys/mman.h> 20 #include <sys/stat.h> 21 #include <string.h> 22 #include <netinet/in.h> 23 #include <inttypes.h> 24 25 #define BPB 8 /* bits/byte */ 26 27 static uint32_t crc32[1<<BPB]; 28 29 static void init_crc32(void) 30 { 31 const uint32_t poly = ntohl(0x2083b8ed); 32 int n; 33 34 for (n = 0; n < 1<<BPB; n++) { 35 uint32_t crc = n; 36 int bit; 37 38 for (bit = 0; bit < BPB; bit++) 39 crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1); 40 crc32[n] = crc; 41 } 42 } 43 44 static uint32_t crc32buf(const unsigned char *buf, size_t len) 45 { 46 uint32_t crc = 0xFFFFFFFF; 47 48 for (; len; len--, buf++) 49 crc = crc32[(uint8_t)crc ^ *buf] ^ (crc >> BPB); 50 return ~crc; 51 } 52 53 /* HDR0 reversed, to be stored as BE */ 54 #define MAGIC 0x30524448 /* HDR0 reversed, to be stored as BE */ 55 56 /* All numbers are stored as BE */ 57 struct zytrx_t { 58 uint32_t magic; 59 uint32_t len_h; /* Length of this header */ 60 uint32_t len_t; /* Total length of file */ 61 uint32_t crc32_p; /* Bit inverted 32-bit CRC of image payload */ 62 uint8_t verInt[32]; /* String "5.0.0.0\n" zero padded */ 63 uint8_t verExt[32]; /* String "\n" zero padded */ 64 uint32_t len_p; /* Length of image payload */ 65 uint8_t pad1[12]; /* zero padding(?) */ 66 uint8_t code[164]; /* string "3 6035 122 0\n" zero padded */ 67 uint8_t chipid[8]; /* string "MT7621A" zero padded */ 68 uint8_t boardid[16]; /* string "NR7101" zero padded */ 69 uint32_t modelid; /* modelid as 4 BCD digits: 0x07010001 */ 70 uint8_t pad2[8]; /* zero padding(?) */ 71 uint8_t swVersionInt[32]; /* ZyXEL version string: "1.00(ABUV.0)D0" zero padded */ 72 uint8_t swVersionExt[32]; /* identical to swVersionInt */ 73 uint8_t pad4[4]; /* zero padding(?) */ 74 uint32_t kernelChksum; /* no idea how this is computed - reported but not validated */ 75 uint8_t pad5[4]; /* zero padding(?) */ 76 uint32_t crc32_h; /* Bit inverted 32-bit CRC of this header payload */ 77 uint8_t pad6[4]; /* zero padding(?) */ 78 }; 79 80 /* static?() field values of unknown meaning - maybe ove to board 81 * table when we know the significance 82 */ 83 #define VER_INT "5.0.0.0\n" 84 #define VER_EXT "\n" 85 #define CODE "3 6035 122 0\n" 86 #define KERNELCHKSUM 0x12345678 87 88 /* table of supported devices using this header format */ 89 static struct board_t { 90 uint8_t chipid[8]; 91 uint8_t boardid[16]; 92 uint32_t modelid; 93 } boards[] = { 94 { "MT7621A", "NR7101", 0x07010001 }, 95 { "MT7621A", "LTE3301-PLUS", 0x03030001 }, 96 { "MT7621A", "LTE5398-M904", 0x05030908 }, 97 {} 98 }; 99 100 static int find_board(struct zytrx_t *h, char *board) 101 { 102 struct board_t *p; 103 104 for (p = boards; p->modelid; p++) { 105 if (strncmp((const char *)p->boardid, board, sizeof(p->boardid))) 106 continue; 107 memcpy(h->chipid, p->chipid, sizeof(h->chipid)); 108 memcpy(h->boardid, p->boardid, sizeof(h->boardid)); 109 h->modelid = htonl(p->modelid); 110 return 0; 111 } 112 return -1; 113 } 114 115 static void usage(const char *name) 116 { 117 struct board_t *p; 118 119 fprintf(stderr, "Usage:\n"); 120 fprintf(stderr, " %s -B <board> -v <versionstr> -i <file> [-o <outputfile>]\n\n", name); 121 fprintf(stderr, "Supported <board> values:\n"); 122 for (p = boards; p->modelid; p++) 123 fprintf(stderr, "\t%-12s\n", p->boardid); 124 fprintf(stderr, "\nExample:\n"); 125 fprintf(stderr, " %s -B %s -v foobar-1.0 -i my.img -o out.img\n\n", name, 126 boards[0].boardid); 127 exit(EXIT_FAILURE); 128 } 129 130 static void errexit(const char *msg) 131 { 132 fprintf(stderr, "ERR: %s: %s\n", msg, errno ? strerror(errno) : "unknown"); 133 exit(EXIT_FAILURE); 134 } 135 136 static void *map_input(const char *name, size_t *len) 137 { 138 struct stat stat; 139 void *mapped; 140 int fd; 141 142 fd = open(name, O_RDONLY); 143 if (fd < 0) 144 return NULL; 145 if (fstat(fd, &stat) < 0) { 146 close(fd); 147 return NULL; 148 } 149 *len = stat.st_size; 150 mapped = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0); 151 if (close(fd) < 0) { 152 (void) munmap(mapped, stat.st_size); 153 return NULL; 154 } 155 return mapped; 156 } 157 158 int main(int argc, char **argv) 159 { 160 int c, fdout = STDOUT_FILENO; 161 void *input_file = NULL; 162 size_t file_len, len; 163 uint32_t crc; 164 struct zytrx_t h = { 165 .magic = htonl(MAGIC), 166 .len_h = htonl(sizeof(h)), 167 .verInt = VER_INT, 168 .verExt = VER_EXT, 169 .code = CODE, 170 .kernelChksum = htonl(KERNELCHKSUM), 171 }; 172 173 while ((c = getopt(argc, argv, "B:v:i:o:")) != -1) { 174 switch (c) { 175 case 'B': 176 if (find_board(&h, optarg) < 0) 177 errexit("unsupported board"); 178 break; 179 case 'v': 180 len = strlen(optarg); 181 if (len > sizeof(h.swVersionInt)) 182 errexit("version string too long"); 183 memcpy(h.swVersionInt, optarg, len); 184 memcpy(h.swVersionExt, optarg, len); 185 break; 186 case 'i': 187 input_file = map_input(optarg, &file_len); 188 if (!input_file) 189 errexit(optarg); 190 break; 191 case 'o': 192 fdout = open(optarg, O_WRONLY | O_CREAT, 0644); 193 if (fdout < 0) 194 errexit(optarg); 195 break; 196 default: 197 usage(argv[0]); 198 } 199 } 200 201 /* required paremeters */ 202 if (!input_file || !h.modelid || !h.swVersionInt[0]) 203 usage(argv[0]); 204 205 /* length fields */ 206 h.len_t = htonl(sizeof(h) + file_len); 207 h.len_p = htonl(file_len); 208 209 /* crc fields */ 210 init_crc32(); 211 crc = crc32buf(input_file, file_len); 212 h.crc32_p = htonl(~crc); 213 crc = crc32buf((unsigned char *)&h, sizeof(h)); 214 h.crc32_h = htonl(~crc); 215 216 /* dump new image */ 217 write(fdout, &h, sizeof(h)); 218 write(fdout, input_file, file_len); 219 220 /* close files */ 221 munmap(input_file, file_len); 222 if (fdout != STDOUT_FILENO) 223 close(fdout); 224 225 return EXIT_SUCCESS; 226 } 227
This page was automatically generated by LXR 0.3.1. • OpenWrt