blob: 1206d1e42af52b30852a39f7e311c3ad9606e6e5 [file] [log] [blame]
Stefan Reinauera2b67d32015-09-01 16:30:43 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
Martin Rothb54d6ba2015-09-29 14:49:37 -060015 * Foundation, Inc.
Stefan Reinauera2b67d32015-09-01 16:30:43 -070016 */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include "em100.h"
23
24/* file format:
25 *
26 * 0x0000000: 65 6d 31 30 30 70 72 6f - magic 8 bytes
27 * 0x0000014: 32 2e 32 36 - version MCU
28 * 0x000001e: 30 2e 37 35 - version FPGA
29 * 0x0000028: 57 46 50 44 - WFPD (??)
30 * 0x0000038: 00 01 00 00 - file offset FPGA
31 * 0x000003c: 44 15 07 00 - file length FPGA
32 * 0x0000040: 00 17 07 00 - file offset MCU
33 * 0x0000044: 00 bf 00 00 - file length MCU
34 *
35 * flash layout
36 * 0x0000000: fpga firmware
37 * 0x0100000: 256 bytes of 0x00 filled space
38 * 0x0100100: mcu firmware
39 * 0x01f0000: 4 bytes secret key, 00 padded
40 * 0x01fff00: ff ff 4 bytes serial number ff padded
41 *
42 * - empty pages remain 0xff filled
43 * - partially used pages are typically filled up with 00 bytes
44 * except the page containing the serial number
45 */
46
47#undef DEBUG
48
49static void print_progress(int percent)
50{
51 int i;
52
53 putchar('\r');
54 putchar('[');
55 for (i = 0; i < percent / 2; i++)
56 putchar('=');
57 for (i = 0; i < 50 - (percent / 2); i++)
58 putchar(' ');
59 putchar(']');
60 if (percent == 100)
61 putchar('\n');
62 fflush(stdout);
63}
64
65static uint32_t get_le32(const unsigned char *in)
66{
67 return (in[3] << 24) | (in[2] << 16) | (in[1] << 8) | (in[0] << 0);
68}
69
Stefan Reinauera9278a92015-09-01 16:16:27 -070070static void put_le32(unsigned char *out, uint32_t val)
71{
72 out[0] = val&0xff;
73 out[1] = (val>>8)&0xff;
74 out[2] = (val>>16)&0xff;
75 out[3] = (val>>24)&0xff;
76}
77
Martin Rothb54d6ba2015-09-29 14:49:37 -060078int firmware_dump(struct em100 *em100, const char *filename,
79 int firmware_is_dpfw)
Stefan Reinauera2b67d32015-09-01 16:30:43 -070080{
81#define ROM_SIZE (2*1024*1024)
82 unsigned char data[ROM_SIZE];
83 int i;
84 FILE *fw;
85
86 printf("\nWriting EM100Pro firmware to file %s\n", filename);
87 memset(data, 0, ROM_SIZE);
88 for (i=0; i < ROM_SIZE; i+=256) {
89 if((i & 0x7fff) == 0)
90 print_progress(i * 100 / ROM_SIZE);
91 if (!read_spi_flash_page(em100, i, data+i)) {
92 if (!read_spi_flash_page(em100, i, data+i))
93 if (!read_spi_flash_page(em100, i, data+i))
Martin Rothb54d6ba2015-09-29 14:49:37 -060094 printf("\nERROR: Couldn't read @%08x\n",
95 i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -070096 }
97 }
98 print_progress(100);
99 fw = fopen(filename, "wb");
Stefan Reinauera9278a92015-09-01 16:16:27 -0700100 if (!fw) {
101 perror(filename);
102 return 0;
103 }
104
105 if (firmware_is_dpfw) {
106 int fpga_size = 0, mcu_size = 0;
107 char *all_ff[256];
108 char mcu_version[8];
109 char fpga_version[8];
110 unsigned char header[0x100];
111
112 memset(all_ff, 255, 256);
113 for (i = 0; i < 0x100000; i+=0x100) {
114 if (memcmp(data+i, all_ff, 256) == 0)
115 break;
116 }
117 if (i == 0x100000) {
118 printf("Can't parse device firmware. Please extract"
119 " raw firmware instead.\n");
120 exit(1);
121 }
122 fpga_size = i;
123
124 for (i = 0; i < 0xfff00; i+=0x100) {
125 if (memcmp(data+0x100100+i, all_ff, 256) == 0)
126 break;
127 }
128 if (i == 0xfff00) {
129 printf("Can't parse device firmware. Please extract"
130 " raw firmware instead.\n");
131 exit(1);
132 }
133 mcu_size = i;
134
135 snprintf(mcu_version, 8, "%d.%d",
136 em100->mcu >> 8, em100->mcu & 0xff);
137 snprintf(fpga_version, 8, "%d.%d",
138 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff);
139
140 memset(header, 0, 0x100);
141 memcpy(header, "em100pro", 8);
142 memcpy(header + 0x28, "WFPD", 4);
143 memcpy(header + 0x14, mcu_version, 4);
144 memcpy(header + 0x1e, fpga_version, 4);
145 put_le32(header + 0x38, 0x100);
146 put_le32(header + 0x3c, fpga_size);
147 put_le32(header + 0x40, 0x100 + fpga_size);
148 put_le32(header + 0x44, 0x100 + mcu_size);
149 fwrite(header, 0x100, 1, fw);
150 fwrite(data, fpga_size, 1, fw);
151 fwrite(data + 0x100100, mcu_size, 1, fw);
152 } else {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700153 if (fwrite(data, ROM_SIZE, 1, fw) != 1)
154 printf("ERROR: Couldn't write %s\n", filename);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700155 }
Stefan Reinauera9278a92015-09-01 16:16:27 -0700156 fclose(fw);
157
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700158#if DEBUG
159 hexdump(data, ROM_SIZE);
160#endif
161 return 0;
162}
163
164int firmware_update(struct em100 *em100, const char *filename, int verify)
165{
166 unsigned char page[256], vpage[256];
167 FILE *f;
168 long fsize;
169 unsigned char *fw;
170 int i;
171
172 printf("\nAttempting firmware update with file %s\n", filename);
173
174 f = fopen(filename, "rb");
175 if (!f) {
176 perror(filename);
177 return 0;
178 }
179
180 fseek(f, 0, SEEK_END);
181 fsize = ftell(f);
182 fseek(f, 0, SEEK_SET);
183
184 fw = malloc(fsize);
185 if (fread(fw, fsize, 1, f) != 1) {
186 perror(filename);
187 fclose(f);
188 return 0;
189 }
190 fclose(f);
191
192 if (memcmp(fw, "em100pro", 8) != 0 ||
193 memcmp(fw + 0x28, "WFPD", 4) != 0) {
194 printf("ERROR: Not an EM100Pro firmware file.\n");
195 free(fw);
196 return 0;
197 }
198
199#define FPGA_OFFSET get_le32(fw+0x38)
200#define FPGA_LEN get_le32(fw+0x3c)
201#define MCU_OFFSET get_le32(fw+0x40)
202#define MCU_LEN get_le32(fw+0x44)
203
204 printf("EM100Pro Update File: %s\n", filename);
205 printf(" Installed version: MCU %d.%d, FPGA %d.%d (%s)\n",
206 em100->mcu >> 8, em100->mcu & 0xff,
207 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff,
208 em100->fpga & 0x8000 ? "1.8V" : "3.3V");
209 printf(" New version: MCU %s, FPGA %s\n", fw + 0x14, fw + 0x1e);
210
211 /* Unlock and erase sector. Reading
212 * the SPI flash ID is requires to
213 * actually unlock the chip.
214 */
215 unlock_spi_flash(em100);
216 get_spi_flash_id(em100);
217
218 printf("Erasing firmware:\n");
219 for (i=0; i<=0x1e; i++) {
220 print_progress(i * 100 / 0x1e);
221 erase_spi_flash_sector(em100, i);
222 }
223 get_spi_flash_id(em100); // Needed?
224
225 printf("Writing firmware:\n");
226
227 /* Writing FPGA firmware */
228 for (i = 0; i < FPGA_LEN; i += 256) {
229 memset(page, 0xff, 256);
230 memcpy(page, fw + FPGA_OFFSET + i, FPGA_LEN - i
231 > 256 ? 256 : FPGA_LEN - i);
232 write_spi_flash_page(em100, i, page);
233 if ((i & 0xfff) == 0)
234 print_progress((i * 100) / (FPGA_LEN + MCU_LEN));
235 }
236
237 /* Writing MCU firmware */
238 for (i = 0; i < MCU_LEN; i += 256) {
239 memset(page, 0xff, 256);
240 memcpy(page, fw + MCU_OFFSET + i, MCU_LEN - i
241 > 256 ? 256 : MCU_LEN - i);
242 write_spi_flash_page(em100, i + 0x100100, page);
243 if ((i & 0xfff) == 0)
Martin Rothb54d6ba2015-09-29 14:49:37 -0600244 print_progress(((FPGA_LEN + i) * 100) /
245 (FPGA_LEN + MCU_LEN));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700246 }
247 print_progress(100);
248
249 if (verify) {
250 printf("Verifying firmware:\n");
251 for (i = 0; i < FPGA_LEN; i += 256) {
252 memset(page, 0xff, 256);
253 memcpy(page, fw + FPGA_OFFSET + i, FPGA_LEN - i
254 > 256 ? 256 : FPGA_LEN - i);
255 read_spi_flash_page(em100, i, vpage);
256
257 if ((i & 0xfff) == 0)
258 print_progress((i * 100) / (FPGA_LEN + MCU_LEN));
259 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600260 printf("\nERROR: Could not write FPGA firmware"
261 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700262 }
263 for (i = 0; i < MCU_LEN; i += 256) {
264 memset(page, 0xff, 256);
265 memcpy(page, fw + MCU_OFFSET + i, MCU_LEN - i
266 > 256 ? 256 : MCU_LEN - i);
267 read_spi_flash_page(em100, i + 0x100100, vpage);
268
269 if ((i & 0xfff) == 0)
Martin Rothb54d6ba2015-09-29 14:49:37 -0600270 print_progress(((FPGA_LEN + i) * 100) /
271 (FPGA_LEN + MCU_LEN));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700272 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600273 printf("\nERROR: Could not write MCU firmware"
274 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700275 }
276 }
277 print_progress(100);
278
279 free(fw);
280
281 /* Write magic update page */
282 memset(page, 0x00, 256);
283 page[0] = 0xaa;
284 page[1] = 0x55;
285 page[2] = 0x42;
286 page[3] = 0x4f;
287 page[4] = 0x4f;
288 page[5] = 0x54;
289 page[6] = 0x55;
290 page[7] = 0xaa;
291 write_spi_flash_page(em100, 0x100000, page);
292
293 if (verify) {
294 read_spi_flash_page(em100, 0x100000, vpage);
295 if (memcmp(page, vpage, 256))
296 printf("ERROR: Could not write update tag.\n");
297 }
298
299 printf("\nDisconnect and reconnect your EM100pro\n");
300
301 return 1;
302}