Changeset 20437
 Timestamp:
 20100326T11:12:30+01:00 (8 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/tools/firmwareutils/src/lzma2eva.c
r18760 r20437 23 23 #include <zlib.h> /* crc32 */ 24 24 25 26 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))27 28 25 #define checksum_add32(csum, data) \ 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) 26 csum += ((uint8_t *)&data)[0]; \ 27 csum += ((uint8_t *)&data)[1]; \ 28 csum += ((uint8_t *)&data)[2]; \ 29 csum += ((uint8_t *)&data)[3]; 35 30 36 31 void … … 48 43 } 49 44 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 109 45 int 110 46 main(int argc, char *argv[]) 111 47 { 48 112 49 const char *infile, *outfile; 113 50 FILE *in, *out; … … 131 68 uint32_t datacrc32 = crc32(0, 0, 0); 132 69 70 uint32_t zero = 0; 133 71 uint32_t entry = 0; 134 72 … … 150 88 151 89 /* read LZMA header */ 152 if ( fread_8(&properties, in))90 if (1 != fread(&properties, sizeof properties, 1, in)) 153 91 pexit("fread"); 154 if ( fread_le32(&dictsize, in))92 if (1 != fread(&dictsize, sizeof dictsize, 1, in)) 155 93 pexit("fread"); 156 if ( fread_le64(&datasize, in))94 if (1 != fread(&datasize, sizeof datasize, 1, in)) 157 95 pexit("fread"); 158 96 159 97 /* write EVA header */ 160 if ( fwrite_le32(magic, out))98 if (1 != fwrite(&magic, sizeof magic, 1, out)) 161 99 pexit("fwrite"); 162 100 if (fgetpos(out, &reclengthpos)) 163 101 pexit("fgetpos"); 164 if ( fwrite_le32(reclength, out))102 if (1 != fwrite(&reclength, sizeof reclength, 1, out)) 165 103 pexit("fwrite"); 166 if ( fwrite_le32(loadaddress, out))104 if (1 != fwrite(&loadaddress, sizeof loadaddress, 1, out)) 167 105 pexit("fwrite"); 168 if ( fwrite_le32(type, out))106 if (1 != fwrite(&type, sizeof type, 1, out)) 169 107 pexit("fwrite"); 170 108 … … 172 110 if (fgetpos(out, &compsizepos)) 173 111 pexit("fgetpos"); 174 if ( fwrite_le32(compsize, out))112 if (1 != fwrite(&compsize, sizeof compsize, 1, out)) 175 113 pexit("fwrite"); 176 114 /* XXX check length */ 177 115 datasize32 = (uint32_t)datasize; 178 if ( fwrite_le32(datasize32, out))116 if (1 != fwrite(&datasize32, sizeof datasize32, 1, out)) 179 117 pexit("fwrite"); 180 if ( fwrite_le32(datacrc32, out))118 if (1 != fwrite(&datacrc32, sizeof datacrc32, 1, out)) 181 119 pexit("fwrite"); 182 120 183 121 /* write modified LZMA header */ 184 if ( fwrite_8(properties, out))122 if (1 != fwrite(&properties, sizeof properties, 1, out)) 185 123 pexit("fwrite"); 186 if ( fwrite_le32(dictsize, out))124 if (1 != fwrite(&dictsize, sizeof dictsize, 1, out)) 187 125 pexit("fwrite"); 188 if ( fwrite_le32(0, out))126 if (1 != fwrite(&zero, 3, 1, out)) 189 127 pexit("fwrite"); 190 128 … … 204 142 if (fsetpos(out, &reclengthpos)) 205 143 pexit("fsetpos"); 206 if ( fwrite_le32(reclength, out))144 if (1 != fwrite(&reclength, sizeof reclength, 1, out)) 207 145 pexit("fwrite"); 208 146 … … 210 148 if (fsetpos(out, &compsizepos)) 211 149 pexit("fsetpos"); 212 if ( fwrite_le32(compsize, out))150 if (1 != fwrite(&compsize, sizeof compsize, 1, out)) 213 151 pexit("fwrite"); 214 if ( fwrite_le32(datasize32, out))152 if (1 != fwrite(&datasize32, sizeof datasize32, 1, out)) 215 153 pexit("fwrite"); 216 if ( fwrite_le32(datacrc32, out))154 if (1 != fwrite(&datacrc32, sizeof datacrc32, 1, out)) 217 155 pexit("fwrite"); 218 156 … … 237 175 238 176 checksum = ~checksum + 1; 239 if ( fwrite_le32(checksum, out))177 if (1 != fwrite(&checksum, sizeof checksum, 1, out)) 240 178 pexit("fwrite"); 241 179 242 180 /* write entry record */ 243 if ( fwrite_le32(0, out))181 if (1 != fwrite(&zero, sizeof zero, 1, out)) 244 182 pexit("fwrite"); 245 if ( fwrite_le32(entry, out))183 if (1 != fwrite(&entry, sizeof entry, 1, out)) 246 184 pexit("fwrite"); 247 185
Note: See TracChangeset
for help on using the changeset viewer.