forked from isogashii/fernly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flashrom-fernvale.patch
349 lines (342 loc) · 8.11 KB
/
flashrom-fernvale.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
This patch adds "fernvale_spi" "programmer" for "flashrom" utility
http://flashrom.org/ . It allows read or program FlashROM on a
Mediatek MT6260 device using "fernly" bootloader
https://github.com/xobs/fernly .
Author: Sean Cross <[email protected]>
Index: Makefile
===================================================================
--- Makefile (revision 1897)
+++ Makefile (working copy)
@@ -272,6 +272,11 @@
else
override CONFIG_BUSPIRATE_SPI = no
endif
+ifeq ($(CONFIG_FERNVALE_SPI), yes)
+UNSUPPORTED_FEATURES += CONFIG_FERNVALE_SPI=yes
+else
+override CONFIG_FERNVALE_SPI = no
+endif
ifeq ($(CONFIG_SERPROG), yes)
UNSUPPORTED_FEATURES += CONFIG_SERPROG=yes
else
@@ -469,6 +474,9 @@
# Always enable Bus Pirate SPI for now.
CONFIG_BUSPIRATE_SPI ?= yes
+# Always enable Fernvale SPI, too
+CONFIG_FERNVALE_SPI ?= yes
+
# Disable Dediprog SF100 until support is complete and tested.
CONFIG_DEDIPROG ?= no
@@ -694,6 +702,12 @@
NEED_SERIAL := yes
endif
+ifeq ($(CONFIG_FERNVALE_SPI), yes)
+FEATURE_CFLAGS += -D'CONFIG_FERNVALE_SPI=1'
+PROGRAMMER_OBJS += fernvale_spi.o
+NEED_SERIAL := yes
+endif
+
ifeq ($(CONFIG_DEDIPROG), yes)
FEATURE_CFLAGS += -D'CONFIG_DEDIPROG=1'
PROGRAMMER_OBJS += dediprog.o
Index: fernvale_spi.c
===================================================================
--- fernvale_spi.c (revision 0)
+++ fernvale_spi.c (working copy)
@@ -0,0 +1,238 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+#include <termios.h>
+
+#include "flash.h"
+#include "programmer.h"
+#include "spi.h"
+
+#define DEFAULT_DEV "/dev/fernvale"
+#define BAUDRATE B921600
+
+static struct {
+ int fd;
+} fernvale_data;
+
+static int fernvale_spi_send_command(struct flashctx *flash,
+ unsigned int writecnt, unsigned int readcnt,
+ const unsigned char *writearr,
+ unsigned char *readarr);
+
+static const struct spi_master spi_master_fernvale = {
+ .type = SPI_CONTROLLER_FERNVALE,
+ .max_data_read = 128,
+ .max_data_write = 128,
+ .command = fernvale_spi_send_command,
+ .multicommand = default_spi_send_multicommand,
+ .read = default_spi_read,
+ .write_256 = default_spi_write_256,
+ .write_aai = default_spi_write_aai,
+};
+
+static int fernvale_spi_shutdown(void *data)
+{
+ const char cmd[] = { 0, 0 };
+
+ write(fernvale_data.fd, cmd, sizeof(cmd));
+
+ return 0;
+}
+
+static int fernvale_spi_setserial(int serfd)
+{
+ int ret;
+ struct termios t;
+
+ ret = tcgetattr(serfd, &t);
+ if (-1 == ret) {
+ perror("Failed to get attributes");
+ exit(1);
+ }
+ cfsetispeed(&t, BAUDRATE);
+ cfsetospeed(&t, BAUDRATE);
+ cfmakeraw(&t);
+ ret = tcsetattr(serfd, TCSANOW, &t);
+ if (-1 == ret) {
+ perror("Failed to set attributes");
+ exit(1);
+ }
+
+ return 0;
+}
+
+int fernvale_spi_init(void)
+{
+ struct spi_master mst = spi_master_fernvale;
+ char *dev;
+
+ dev = extract_programmer_param("dev");
+ if (dev && !strlen(dev)) {
+ free(dev);
+ dev = NULL;
+ }
+ if (!dev)
+ dev = DEFAULT_DEV;
+
+ fernvale_data.fd = open(dev, O_RDWR);
+ if (fernvale_data.fd == -1) {
+ msg_perr("Unable to open serial device. "
+ "Use flashrom -p fernvale_spi:dev=/dev/ttyUSB0\n");
+ return 1;
+ }
+
+ fernvale_spi_setserial(fernvale_data.fd);
+
+ const char cmd[] = "spi flashrom\n";
+ char readback;
+ int ready_tries = 0;
+ write(fernvale_data.fd, cmd, strlen(cmd));
+
+ /* Look for "Ready" signal */
+ do {
+ read(fernvale_data.fd, &readback, sizeof(readback));
+ ready_tries++;
+ } while (readback != 0x05);
+ msg_gdbg("Found 'ready' signal after %d bytes\n", ready_tries);
+
+ mst.data = &fernvale_data;
+ register_spi_master(&mst);
+ register_shutdown(fernvale_spi_shutdown, NULL);
+
+ return 0;
+}
+
+static int write_full(int fd, const void *bfr, int size)
+{
+ int ret;
+ int left = size;
+
+ while (left > 0) {
+ ret = write(fd, bfr, left);
+ if (ret == -1) {
+ if (errno == EAGAIN)
+ continue;
+ return ret;
+ }
+
+ /* FD closed */
+ if (!ret)
+ return -1;
+
+ left -= ret;
+ bfr += ret;
+ }
+ return size;
+}
+
+static int read_full(int fd, void *bfr, int size)
+{
+ int ret;
+ int left = size;
+
+ msg_gdbg(" Reading %d bytes:", size);
+ while (left > 0) {
+ ret = read(fd, bfr, 1);
+ if (ret == -1) {
+ if (errno == EAGAIN)
+ continue;
+ return ret;
+ }
+ msg_gdbg(" 0x%02x:", *((uint8_t *)bfr));
+
+ /* FD closed */
+ if (!ret)
+ return -1;
+
+ left -= ret;
+ bfr += ret;
+ }
+ return size;
+}
+
+static int fernvale_spi_send_command(struct flashctx *flash,
+ unsigned int writecnt, unsigned int readcnt,
+ const unsigned char *writearr,
+ unsigned char *readarr)
+{
+ uint8_t out_bytes = writecnt;
+ uint8_t in_bytes = readcnt;
+ int ret;
+ int fd = fernvale_data.fd;
+ int i;
+#if 1
+ ret = write_full(fd, &out_bytes, sizeof(out_bytes));
+ if (ret != sizeof(out_bytes))
+ msg_perr("0: Wanted to write %d bytes, but got %d\n",
+ (int)sizeof(out_bytes), ret);
+
+ ret = write_full(fd, &in_bytes, sizeof(in_bytes));
+ if (ret != sizeof(in_bytes))
+ msg_perr("1: Wanted to write %d bytes, but got %d\n",
+ (int)sizeof(in_bytes), ret);
+
+ ret = write_full(fd, writearr, out_bytes);
+ if (ret != out_bytes)
+ msg_perr("0: Wanted to write %d bytes, but got %d\n",
+ out_bytes, ret);
+
+ msg_gdbg(" Wrote %d bytes:", out_bytes);
+ for (i = 0; i < out_bytes; i++)
+ msg_gdbg(" %02x", writearr[i]);
+ msg_gdbg(" ");
+#else
+ uint8_t bfr[writecnt + 2];
+
+ bfr[0] = out_bytes;
+ bfr[1] = in_bytes;
+ memcpy(&bfr[2], writearr, out_bytes);
+
+ ret = write_full(fd, bfr, sizeof(bfr));
+ if (ret != sizeof(bfr))
+ msg_perr("0: Wanted to write %d bytes, but got %d\n",
+ sizeof(bfr), ret);
+
+ msg_gdbg(" Wrote %d bytes:", sizeof(bfr));
+ for (i = 0; i < sizeof(bfr); i++)
+ msg_gdbg(" %02x", bfr[i]);
+ msg_gdbg(" ");
+#endif
+
+ ret = read_full(fd, readarr, in_bytes);
+ if (ret != in_bytes)
+ msg_perr("3: Wanted to read %d bytes, but got %d\n",
+ in_bytes, ret);
+
+ msg_gdbg(" Read %d bytes:", in_bytes);
+ for (i = 0; i < in_bytes; i++)
+ msg_gdbg(" %02x", readarr[i]);
+ msg_gdbg(" ");
+
+ msg_gdbg("\n");
+
+ return 0;
+}
Index: flashrom.c
===================================================================
--- flashrom.c (revision 1897)
+++ flashrom.c (working copy)
@@ -231,6 +231,19 @@
},
#endif
+#if CONFIG_FERNVALE_SPI == 1
+ {
+ .name = "fernvale_spi",
+ .type = OTHER,
+ /* FIXME */
+ .devs.note = "Kosagi Fernvale\n",
+ .init = fernvale_spi_init,
+ .map_flash_region = fallback_map,
+ .unmap_flash_region = fallback_unmap,
+ .delay = internal_delay,
+ },
+#endif
+
#if CONFIG_DEDIPROG == 1
{
.name = "dediprog",
Index: programmer.h
===================================================================
--- programmer.h (revision 1897)
+++ programmer.h (working copy)
@@ -69,6 +69,9 @@
#if CONFIG_BUSPIRATE_SPI == 1
PROGRAMMER_BUSPIRATE_SPI,
#endif
+#if CONFIG_FERNVALE_SPI == 1
+ PROGRAMMER_FERNVALE_SPI,
+#endif
#if CONFIG_DEDIPROG == 1
PROGRAMMER_DEDIPROG,
#endif
@@ -506,6 +509,11 @@
int buspirate_spi_init(void);
#endif
+/* fernvale_spi.c */
+#if CONFIG_FERNVALE_SPI == 1
+int fernvale_spi_init(void);
+#endif
+
/* linux_spi.c */
#if CONFIG_LINUX_SPI == 1
int linux_spi_init(void);
@@ -554,6 +562,9 @@
#if CONFIG_BUSPIRATE_SPI == 1
SPI_CONTROLLER_BUSPIRATE,
#endif
+#if CONFIG_FERNVALE_SPI == 1
+ SPI_CONTROLLER_FERNVALE,
+#endif
#if CONFIG_DEDIPROG == 1
SPI_CONTROLLER_DEDIPROG,
#endif