blob: 0e9639b5c904a749562d608d946ac6a1b4ed6e75 [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
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
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
70int firmware_dump(struct em100 *em100, const char *filename)
71{
72#define ROM_SIZE (2*1024*1024)
73 unsigned char data[ROM_SIZE];
74 int i;
75 FILE *fw;
76
77 printf("\nWriting EM100Pro firmware to file %s\n", filename);
78 memset(data, 0, ROM_SIZE);
79 for (i=0; i < ROM_SIZE; i+=256) {
80 if((i & 0x7fff) == 0)
81 print_progress(i * 100 / ROM_SIZE);
82 if (!read_spi_flash_page(em100, i, data+i)) {
83 if (!read_spi_flash_page(em100, i, data+i))
84 if (!read_spi_flash_page(em100, i, data+i))
85 printf("\nERROR: Couldn't read @%08x\n", i);
86 }
87 }
88 print_progress(100);
89 fw = fopen(filename, "wb");
90 if (fw) {
91 if (fwrite(data, ROM_SIZE, 1, fw) != 1)
92 printf("ERROR: Couldn't write %s\n", filename);
93 fclose(fw);
94 }
95#if DEBUG
96 hexdump(data, ROM_SIZE);
97#endif
98 return 0;
99}
100
101int firmware_update(struct em100 *em100, const char *filename, int verify)
102{
103 unsigned char page[256], vpage[256];
104 FILE *f;
105 long fsize;
106 unsigned char *fw;
107 int i;
108
109 printf("\nAttempting firmware update with file %s\n", filename);
110
111 f = fopen(filename, "rb");
112 if (!f) {
113 perror(filename);
114 return 0;
115 }
116
117 fseek(f, 0, SEEK_END);
118 fsize = ftell(f);
119 fseek(f, 0, SEEK_SET);
120
121 fw = malloc(fsize);
122 if (fread(fw, fsize, 1, f) != 1) {
123 perror(filename);
124 fclose(f);
125 return 0;
126 }
127 fclose(f);
128
129 if (memcmp(fw, "em100pro", 8) != 0 ||
130 memcmp(fw + 0x28, "WFPD", 4) != 0) {
131 printf("ERROR: Not an EM100Pro firmware file.\n");
132 free(fw);
133 return 0;
134 }
135
136#define FPGA_OFFSET get_le32(fw+0x38)
137#define FPGA_LEN get_le32(fw+0x3c)
138#define MCU_OFFSET get_le32(fw+0x40)
139#define MCU_LEN get_le32(fw+0x44)
140
141 printf("EM100Pro Update File: %s\n", filename);
142 printf(" Installed version: MCU %d.%d, FPGA %d.%d (%s)\n",
143 em100->mcu >> 8, em100->mcu & 0xff,
144 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff,
145 em100->fpga & 0x8000 ? "1.8V" : "3.3V");
146 printf(" New version: MCU %s, FPGA %s\n", fw + 0x14, fw + 0x1e);
147
148 /* Unlock and erase sector. Reading
149 * the SPI flash ID is requires to
150 * actually unlock the chip.
151 */
152 unlock_spi_flash(em100);
153 get_spi_flash_id(em100);
154
155 printf("Erasing firmware:\n");
156 for (i=0; i<=0x1e; i++) {
157 print_progress(i * 100 / 0x1e);
158 erase_spi_flash_sector(em100, i);
159 }
160 get_spi_flash_id(em100); // Needed?
161
162 printf("Writing firmware:\n");
163
164 /* Writing FPGA firmware */
165 for (i = 0; i < FPGA_LEN; i += 256) {
166 memset(page, 0xff, 256);
167 memcpy(page, fw + FPGA_OFFSET + i, FPGA_LEN - i
168 > 256 ? 256 : FPGA_LEN - i);
169 write_spi_flash_page(em100, i, page);
170 if ((i & 0xfff) == 0)
171 print_progress((i * 100) / (FPGA_LEN + MCU_LEN));
172 }
173
174 /* Writing MCU firmware */
175 for (i = 0; i < MCU_LEN; i += 256) {
176 memset(page, 0xff, 256);
177 memcpy(page, fw + MCU_OFFSET + i, MCU_LEN - i
178 > 256 ? 256 : MCU_LEN - i);
179 write_spi_flash_page(em100, i + 0x100100, page);
180 if ((i & 0xfff) == 0)
181 print_progress(((FPGA_LEN + i) * 100) / (FPGA_LEN + MCU_LEN));
182 }
183 print_progress(100);
184
185 if (verify) {
186 printf("Verifying firmware:\n");
187 for (i = 0; i < FPGA_LEN; i += 256) {
188 memset(page, 0xff, 256);
189 memcpy(page, fw + FPGA_OFFSET + i, FPGA_LEN - i
190 > 256 ? 256 : FPGA_LEN - i);
191 read_spi_flash_page(em100, i, vpage);
192
193 if ((i & 0xfff) == 0)
194 print_progress((i * 100) / (FPGA_LEN + MCU_LEN));
195 if (memcmp(page, vpage, 256))
196 printf("\nERROR: Could not write FPGA firmware (%x).\n", i);
197 }
198 for (i = 0; i < MCU_LEN; i += 256) {
199 memset(page, 0xff, 256);
200 memcpy(page, fw + MCU_OFFSET + i, MCU_LEN - i
201 > 256 ? 256 : MCU_LEN - i);
202 read_spi_flash_page(em100, i + 0x100100, vpage);
203
204 if ((i & 0xfff) == 0)
205 print_progress(((FPGA_LEN + i) * 100) / (FPGA_LEN + MCU_LEN));
206 if (memcmp(page, vpage, 256))
207 printf("\nERROR: Could not write MCU firmware (%x).\n", i);
208 }
209 }
210 print_progress(100);
211
212 free(fw);
213
214 /* Write magic update page */
215 memset(page, 0x00, 256);
216 page[0] = 0xaa;
217 page[1] = 0x55;
218 page[2] = 0x42;
219 page[3] = 0x4f;
220 page[4] = 0x4f;
221 page[5] = 0x54;
222 page[6] = 0x55;
223 page[7] = 0xaa;
224 write_spi_flash_page(em100, 0x100000, page);
225
226 if (verify) {
227 read_spi_flash_page(em100, 0x100000, vpage);
228 if (memcmp(page, vpage, 256))
229 printf("ERROR: Could not write update tag.\n");
230 }
231
232 printf("\nDisconnect and reconnect your EM100pro\n");
233
234 return 1;
235}