1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2014 Soul Trace <S-trace@list.ru> 4 */ 5 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <sys/types.h> 9 #include <string.h> 10 #include <unistd.h> 11 #if defined(__linux__) 12 #include <endian.h> 13 #else 14 #include <sys/endian.h> 15 #endif 16 17 #define szbuf 32768 18 19 u_int32_t crc_tab[256]; 20 21 u_int32_t chksum_crc32 (FILE *f) 22 { 23 register unsigned long crc; 24 unsigned long i, j; 25 char *buffer = malloc(szbuf); 26 char *buf; 27 28 crc = 0xFFFFFFFF; 29 while (!feof(f)) 30 { 31 j = fread(buffer, 1, szbuf, f); 32 buf = buffer; 33 for (i = 0; i < j; i++) 34 crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab[(crc ^ *buf++) & 0xFF]; 35 } 36 free(buffer); 37 return crc; 38 } 39 40 void chksum_crc32gentab () 41 { 42 unsigned long crc, poly; 43 int i, j; 44 45 poly = 0xEDB88320L; 46 for (i = 0; i < 256; i++) 47 { 48 crc = i; 49 for (j = 8; j > 0; j--) 50 { 51 if (crc & 1) 52 crc = (crc >> 1) ^ poly; 53 else 54 crc >>= 1; 55 } 56 crc_tab[i] = crc; 57 } 58 } 59 60 void usage(char *progname) 61 { 62 printf("Usage: %s [ -v Version ] [ -d Device_ID ] <input file>\n", progname); 63 exit(1); 64 } 65 66 int main(int argc, char *argv[]) { 67 struct signature 68 { 69 const char magic[4]; 70 unsigned int device_id; 71 char firmware_version[48]; 72 unsigned int crc32; 73 } 74 sign = 75 { 76 { 'Z', 'N', 'B', 'G' }, 77 1, 78 { "V.1.0.0(1.0.0)" }, 79 0 80 }; 81 FILE *f; 82 struct signature oldsign; 83 char *filename; 84 static const char *optString; 85 int opt; 86 87 if (argc < 1) 88 usage(argv[0]); 89 90 optString = "v:d:h"; 91 opt = getopt( argc, argv, optString ); 92 while( opt != -1 ) { 93 switch( opt ) { 94 case 'v': 95 if (optarg == NULL) 96 usage(argv[0]); 97 strncpy(sign.firmware_version, optarg, sizeof(sign.firmware_version)-1); 98 sign.firmware_version[sizeof(sign.firmware_version)-1]='\0'; /* Make sure that string is terminated correctly */ 99 break; 100 101 case 'd': 102 sign.device_id = atoi(optarg); 103 if (sign.device_id == 0) 104 sign.device_id = (int)strtol(optarg, NULL, 16); 105 break; 106 107 case '?': 108 case 'h': 109 usage(argv[0]); 110 break; 111 112 default: 113 break; 114 } 115 116 opt = getopt( argc, argv, optString ); 117 } 118 119 chksum_crc32gentab(); 120 121 filename=argv[optind]; 122 if (access(filename, W_OK) || access(filename, R_OK)) 123 { 124 printf("Not open input file %s\n", filename); 125 exit(1); 126 } 127 f = fopen(argv[optind], "r+"); 128 if (f != NULL) 129 { 130 fseek(f, sizeof(sign)*-1, SEEK_END); 131 fread(&oldsign, sizeof(oldsign), 1, f); 132 133 if (strncmp(oldsign.magic,"ZNBG", sizeof(oldsign.magic)) == 0 ) 134 { 135 printf("Image is already signed as:\nDevice ID: 0x%08x\nFirmware version: %s\nImage CRC32: 0x%x\n", oldsign.device_id, oldsign.firmware_version, oldsign.crc32); 136 exit(0); 137 } 138 139 fseek(f, 0, SEEK_SET); 140 /* 141 * On-disk fields are little-endian, matching every existing 142 * ZYIMAGE_ID value in the OpenWrt tree (unlike the vendor tool, 143 * which writes device_id big-endian). E.g. ZYIMAGE_ID 0x804410 and 144 * the vendor's CONFIG_TARGET_DEVICE_ID="0x10448000" are the same 145 * four bytes in opposite order. 146 */ 147 sign.device_id = htole32(sign.device_id); 148 sign.crc32 = htole32(chksum_crc32(f)); 149 fwrite(&sign, sizeof(sign), 1, f); 150 fclose(f); 151 152 printf("Image signed as:\nDevice ID: 0x%08x\nFirmware version: %s\nImage CRC32: 0x%x\n", sign.device_id, sign.firmware_version, sign.crc32); 153 } 154 return 0; 155 } 156
This page was automatically generated by LXR 0.3.1. • OpenWrt