Changeset 18760
 Timestamp:
 20091212T00:50:51+01:00 (8 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/tools/firmwareutils/src/lzma2eva.c
r9401 r18760 23 23 #include <zlib.h> /* crc32 */ 24 24 25 26 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 27 25 28 #define checksum_add32(csum, data) \ 26 csum += ((uint8_t *)&data)[0]; \ 27 csum += ((uint8_t *)&data)[1]; \ 28 csum += ((uint8_t *)&data)[2]; \ 29 csum += ((uint8_t *)&data)[3]; 29 do { \ 30 csum += (((data) >> 0) & 0x000000FF); \ 31 csum += (((data) >> 8) & 0x000000FF); \ 32 csum += (((data) >> 16) & 0x000000FF); \ 33 csum += (((data) >> 24) & 0x000000FF); \ 34 } while (0) 30 35 31 36 void … … 43 48 } 44 49 50 /* Read an 8bit value */ 51 static int fread_8(uint8_t *buf, FILE *fd) 52 { 53 return (fread(buf, sizeof(*buf), 1, fd) == 1) ? 0 : 1; 54 } 55 56 /* Read a 32bit little endian value and convert to host endianness. */ 57 static int fread_le32(uint32_t *buf, FILE *fd) 58 { 59 size_t count; 60 uint8_t tmp[4]; 61 unsigned int i; 62 63 if (fread(tmp, sizeof(tmp), 1, fd) != 1) 64 return 1; 65 *buf = 0; 66 for (i = 0; i < ARRAY_SIZE(tmp); i++) 67 *buf = (uint32_t)(tmp[i]) << (i * 8); 68 69 return 0; 70 } 71 72 /* Read a 64bit little endian value and convert to host endianness. */ 73 static int fread_le64(uint64_t *buf, FILE *fd) 74 { 75 size_t count; 76 uint8_t tmp[8]; 77 unsigned int i; 78 79 if (fread(tmp, sizeof(tmp), 1, fd) != 1) 80 return 1; 81 *buf = 0; 82 for (i = 0; i < ARRAY_SIZE(tmp); i++) 83 *buf = (uint64_t)(tmp[i]) << (i * 8); 84 85 return 0; 86 } 87 88 /* Write an 8bit value */ 89 static int fwrite_8(uint8_t buf, FILE *fd) 90 { 91 return (fwrite(&buf, sizeof(buf), 1, fd) == 1) ? 0 : 1; 92 } 93 94 /* Convert to little endian and write a 32bit value */ 95 static int fwrite_le32(uint32_t buf, FILE *fd) 96 { 97 size_t count; 98 uint8_t tmp[4]; 99 unsigned int i; 100 101 for (i = 0; i < ARRAY_SIZE(tmp); i++) 102 tmp[i] = buf >> (i * 8); 103 if (fwrite(tmp, sizeof(tmp), 1, fd) != 1) 104 return 1; 105 106 return 0; 107 } 108 45 109 int 46 110 main(int argc, char *argv[]) 47 111 { 48 49 112 const char *infile, *outfile; 50 113 FILE *in, *out; … … 68 131 uint32_t datacrc32 = crc32(0, 0, 0); 69 132 70 uint32_t zero = 0;71 133 uint32_t entry = 0; 72 134 … … 88 150 89 151 /* read LZMA header */ 90 if ( 1 != fread(&properties, sizeof properties, 1, in))91 pexit("fread"); 92 if ( 1 != fread(&dictsize, sizeof dictsize, 1, in))93 pexit("fread"); 94 if ( 1 != fread(&datasize, sizeof datasize, 1, in))152 if (fread_8(&properties, in)) 153 pexit("fread"); 154 if (fread_le32(&dictsize, in)) 155 pexit("fread"); 156 if (fread_le64(&datasize, in)) 95 157 pexit("fread"); 96 158 97 159 /* write EVA header */ 98 if ( 1 != fwrite(&magic, sizeof magic, 1, out))160 if (fwrite_le32(magic, out)) 99 161 pexit("fwrite"); 100 162 if (fgetpos(out, &reclengthpos)) 101 163 pexit("fgetpos"); 102 if ( 1 != fwrite(&reclength, sizeof reclength, 1, out))103 pexit("fwrite"); 104 if ( 1 != fwrite(&loadaddress, sizeof loadaddress, 1, out))105 pexit("fwrite"); 106 if ( 1 != fwrite(&type, sizeof type, 1, out))164 if (fwrite_le32(reclength, out)) 165 pexit("fwrite"); 166 if (fwrite_le32(loadaddress, out)) 167 pexit("fwrite"); 168 if (fwrite_le32(type, out)) 107 169 pexit("fwrite"); 108 170 … … 110 172 if (fgetpos(out, &compsizepos)) 111 173 pexit("fgetpos"); 112 if ( 1 != fwrite(&compsize, sizeof compsize, 1, out))174 if (fwrite_le32(compsize, out)) 113 175 pexit("fwrite"); 114 176 /* XXX check length */ 115 177 datasize32 = (uint32_t)datasize; 116 if ( 1 != fwrite(&datasize32, sizeof datasize32, 1, out))117 pexit("fwrite"); 118 if ( 1 != fwrite(&datacrc32, sizeof datacrc32, 1, out))178 if (fwrite_le32(datasize32, out)) 179 pexit("fwrite"); 180 if (fwrite_le32(datacrc32, out)) 119 181 pexit("fwrite"); 120 182 121 183 /* write modified LZMA header */ 122 if ( 1 != fwrite(&properties, sizeof properties, 1, out))123 pexit("fwrite"); 124 if ( 1 != fwrite(&dictsize, sizeof dictsize, 1, out))125 pexit("fwrite"); 126 if ( 1 != fwrite(&zero, 3, 1, out))184 if (fwrite_8(properties, out)) 185 pexit("fwrite"); 186 if (fwrite_le32(dictsize, out)) 187 pexit("fwrite"); 188 if (fwrite_le32(0, out)) 127 189 pexit("fwrite"); 128 190 … … 142 204 if (fsetpos(out, &reclengthpos)) 143 205 pexit("fsetpos"); 144 if ( 1 != fwrite(&reclength, sizeof reclength, 1, out))206 if (fwrite_le32(reclength, out)) 145 207 pexit("fwrite"); 146 208 … … 148 210 if (fsetpos(out, &compsizepos)) 149 211 pexit("fsetpos"); 150 if ( 1 != fwrite(&compsize, sizeof compsize, 1, out))151 pexit("fwrite"); 152 if ( 1 != fwrite(&datasize32, sizeof datasize32, 1, out))153 pexit("fwrite"); 154 if ( 1 != fwrite(&datacrc32, sizeof datacrc32, 1, out))212 if (fwrite_le32(compsize, out)) 213 pexit("fwrite"); 214 if (fwrite_le32(datasize32, out)) 215 pexit("fwrite"); 216 if (fwrite_le32(datacrc32, out)) 155 217 pexit("fwrite"); 156 218 … … 175 237 176 238 checksum = ~checksum + 1; 177 if ( 1 != fwrite(&checksum, sizeof checksum, 1, out))239 if (fwrite_le32(checksum, out)) 178 240 pexit("fwrite"); 179 241 180 242 /* write entry record */ 181 if ( 1 != fwrite(&zero, sizeof zero, 1, out))182 pexit("fwrite"); 183 if ( 1 != fwrite(&entry, sizeof entry, 1, out))243 if (fwrite_le32(0, out)) 244 pexit("fwrite"); 245 if (fwrite_le32(entry, out)) 184 246 pexit("fwrite"); 185 247
Note: See TracChangeset
for help on using the changeset viewer.