10 #if !defined(__BYTE_ORDER)
11 #error "Unknown byte order"
14 #if __BYTE_ORDER == __BIG_ENDIAN
15 #define cpu_to_be32(x) (x)
16 #elif __BYTE_ORDER == __LITTLE_ENDIAN
17 #define cpu_to_be32(x) bswap_32(x)
19 #error "Unsupported endianness"
25 #define DBG(...) {printf(__VA_ARGS__); }
30 #define ERR(...) {printf(__VA_ARGS__); }
33 * Fw Header Layout for Netgear / Sercomm devices (bytes)
35 * Size : 512 bytes + zipped image size
42 * swVer : 55-62 uint32_t in BE
44 * ChkSum : 511 Inverse value of the full image checksum while this location is 0x00
46 static const char* magic = "sErCoMm"; /* 7 */
47 static const unsigned char version[4] = { 0x00, 0x01, 0x00, 0x00 };
48 static const int header_sz = 512;
49 static const int footer_sz = 71;
51 static int is_header = 1;
54 char* file_name; /* name of the file */
55 char* file_data; /* data of the file in memory */
56 u_int32_t file_size; /* length of the file */
59 static u_int8_t getCheckSum(char* data, int len) {
64 ERR("Invalid pointer provided!\n");
68 for (i = 0; i < len; i++) {
76 * read file into buffer
77 * add space for header/footer
79 static int copyToOutputBuf(struct file_info* finfo) {
87 if (!finfo || !finfo->file_name) {
88 ERR("Invalid pointer provided!\n");
92 DBG("Opening file: %s\n", finfo->file_name);
94 if (!(fp = fopen(finfo->file_name, "rb"))) {
95 ERR("Error opening file: %s\n", finfo->file_name);
101 fseek(fp, 0L, SEEK_END);
106 ERR("Error getting filesize: %s\n", finfo->file_name);
112 extra_sz = header_sz;
116 extra_sz = footer_sz;
121 DBG("Filesize: %i\n", file_sz);
122 finfo->file_size = file_sz + extra_sz;
124 if (!(finfo->file_data = malloc(finfo->file_size))) {
125 ERR("Out of memory!\n");
130 /* init header/footer bytes */
131 memset(finfo->file_data + hdr_pos, 0, extra_sz);
133 /* read file and take care of leading header if exists */
134 if (fread(finfo->file_data + img_pos, 1, file_sz, fp) != file_sz) {
135 ERR("Error reading file %s\n", finfo->file_name);
140 DBG("File: read successful\n");
146 static int writeFile(struct file_info* finfo) {
149 if (!finfo || !finfo->file_name) {
150 ERR("Invalid pointer provided!\n");
154 DBG("Opening file: %s\n", finfo->file_name);
156 if (!(fp = fopen(finfo->file_name, "w"))) {
157 ERR("Error opening file: %s\n", finfo->file_name);
161 DBG("Writing file: %s\n", finfo->file_name);
163 if (fwrite(finfo->file_data, 1, finfo->file_size, fp) != finfo->file_size) {
164 ERR("Wanted to write, but something went wrong!\n");
173 static void usage(char* argv[]) {
174 printf("Usage: %s [OPTIONS...]\n"
177 " -f add sercom footer (if absent, header)\n"
178 " -b <hwid> use hardware id specified with <hwid> (ASCII)\n"
179 " -r <hwrev> use hardware revision specified with <hwrev> (ASCII)\n"
180 " -v <version> set image version to <version> (decimal, hex or octal notation)\n"
181 " -i <file> input file\n"
185 int main(int argc, char* argv[]) {
186 struct file_info image = { 0 };
197 c = getopt(argc, argv, "b:i:r:v:f");
209 image.file_name = optarg;
215 swVer = (u_int32_t) strtol(optarg, NULL, 0);
216 swVer = cpu_to_be32(swVer);
224 if (!hwID || !hwVer || !image.file_name) {
230 * copy input to buffer, add extra space for header/footer and return
233 hdr_offset = copyToOutputBuf(&image);
237 DBG("Filling header: %s %s %2X %s\n", hwID, hwVer, swVer, magic);
239 strncpy(image.file_data + hdr_offset + 0, magic, 7);
240 memcpy(image.file_data + hdr_offset + 7, version, sizeof(version));
241 strncpy(image.file_data + hdr_offset + 11, hwID, 34);
242 strncpy(image.file_data + hdr_offset + 45, hwVer, 10);
243 memcpy(image.file_data + hdr_offset + 55, &swVer, sizeof(swVer));
244 strncpy(image.file_data + hdr_offset + 63, magic, 7);
246 /* calculate checksum and invert checksum */
248 chkSum = getCheckSum(image.file_data, image.file_size);
249 chkSum = (chkSum ^ 0xFF) + 1;
250 DBG("Checksum for Image: %hhX\n", chkSum);
252 /* write checksum to header */
253 image.file_data[511] = (char) chkSum;
256 /* overwrite input file */
257 if (writeFile(&image))