1 /* 2 * Linksys e8350 v1 firmware header generator 3 */ 4 5 #include <stdbool.h> 6 #include <stdio.h> 7 #include <string.h> 8 #include <stdlib.h> 9 #include <fcntl.h> 10 #include <unistd.h> 11 #include <sys/stat.h> 12 #include <stdarg.h> 13 #include <getopt.h> 14 #include <time.h> 15 #include <sys/time.h> 16 #include "cyg_crc.h" 17 18 #define AC2350 20 19 #define CYBERTAN_VERSION "v1.0.03" 20 #define SERIAL_NUMBER "003" 21 #define MINOR_VERSION "" 22 #define BUILD_KEYWORD " B" 23 #define BUILD_NUMBER SERIAL_NUMBER 24 #define BETA_VERSION " " 25 #define CYBERTAN_UBOOT_VERSION "v1.0" 26 27 /* add for AC2350 F/W header */ 28 #define FWHDR_MAGIC_STR "CHDR" 29 #define FWHDR_MAGIC 0X52444843 30 31 struct cbt_fw_header { 32 unsigned int magic; /* "CHDR" */ 33 unsigned int len; /* Length of file including header */ 34 unsigned int crc32; /* 32-bit CRC */ 35 unsigned int res; 36 }; 37 38 #define MAX_BUF 1024 39 /* Initial CRC32 checksum value */ 40 #define CRC32_INIT_VALUE 0xffffffff 41 42 int fd, fd_w; 43 44 void die(const char * str, ...) 45 { 46 va_list args; 47 va_start(args, str); 48 vfprintf(stderr, str, args); 49 fputc('\n', stderr); 50 exit(1); 51 } 52 53 int fill_null0(int size) 54 { 55 unsigned char buf[1]; 56 int i; 57 58 fprintf(stderr,"Fill null\n"); 59 60 buf[0] = 0xff; 61 for (i = 0 ; i < size; i++) 62 if (write(fd_w, buf, 1) != 1) 63 return 0; 64 65 return 1; 66 } 67 68 long file_open(const char *name) 69 { 70 struct stat sb; 71 if ((fd = open(name, O_RDONLY, 0)) < 0) 72 die("Unable to open `%s' : %m", name); 73 74 if (fstat (fd, &sb)) 75 die("Unable to stat `%s' : %m", name); 76 77 return sb.st_size; 78 } 79 80 void usage(void) 81 { 82 die("Usage: addfwhdr [-i|--input] sysupgrade.o [-o|--output] code.bin\n"); 83 } 84 85 int main(int argc, char ** argv) 86 { 87 char *input_file = NULL, *output_file = NULL; 88 extern int optind, opterr, optopt; 89 unsigned int input_size,c; 90 int option_index = 0; 91 extern char *optarg; 92 char *buf = NULL; 93 int garbage = 0; 94 int opt; 95 96 struct cbt_fw_header *fwhdr; 97 unsigned int crc; 98 99 static struct option long_options[] = { 100 {"input", 1, 0, 'i'}, 101 {"output", 1, 0, 'o'}, 102 {"garbage", 0, 0, 'g'}, 103 {0, 0, 0, 0} 104 }; 105 106 while(true) { 107 opt = getopt_long(argc, argv, "i:o:g",long_options, &option_index); 108 if (opt == -1) 109 break; 110 switch(opt){ 111 case 'h' : 112 usage(); 113 break; 114 case 'i' : 115 input_file = optarg; 116 printf("input file is [%s]\n", input_file); 117 break; 118 case 'o' : 119 output_file = optarg; 120 printf("output file is [%s]\n", output_file); 121 break; 122 case 'g' : 123 garbage = 1; 124 break; 125 default : 126 usage(); 127 } 128 } 129 130 if (!input_file || !output_file) { 131 printf("You must specify the input and output file!\n"); 132 usage(); 133 } 134 135 unlink(output_file); 136 if ((fd_w = open(output_file, O_RDWR|O_CREAT, S_IREAD | S_IWRITE)) < 0) 137 die("Unable to open `%s' : %m", output_file); 138 139 printf("\n---------- add fw header --------\n"); 140 141 fwhdr = calloc(1, sizeof(struct cbt_fw_header)); 142 memcpy((char *)&fwhdr->magic, FWHDR_MAGIC_STR, sizeof(fwhdr->magic)); 143 144 input_size = file_open(input_file); 145 if (!(buf = malloc(input_size))){ 146 perror("malloc"); 147 goto fail; 148 } 149 c = read(fd, buf, input_size); 150 fwhdr->len = input_size + sizeof(struct cbt_fw_header); 151 fwhdr->res = fwhdr->res | 0x1; 152 153 crc = cyg_crc32_accumulate(CRC32_INIT_VALUE, (uint8_t *)&fwhdr->res, 4); 154 crc = cyg_crc32_accumulate(crc, (uint8_t *)&buf[0], input_size); 155 156 fwhdr->crc32 = crc; 157 158 /* write code pattern header */ 159 write(fd_w, fwhdr, sizeof(struct cbt_fw_header)); 160 161 if (write(fd_w, buf, c) != c) 162 die("Write call failed!\n"); 163 164 fail: 165 free(fwhdr); 166 if (buf) 167 free(buf); 168 close(fd); 169 close(fd_w); 170 171 return 0; 172 } 173
This page was automatically generated by LXR 0.3.1. • OpenWrt