-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflash.c
192 lines (164 loc) · 5.8 KB
/
flash.c
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
/* Copyright (c) 2015 Antumbra
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, either version 3 of the License, or
(at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
#include "libantumbra.h"
#include <string.h>
static AnError helpfully_invoke_cmd(AnCtx *ctx, AnDevice *dev, uint32_t api, uint16_t cmd,
const void *cmddata, unsigned int cmddata_sz,
void *rspdata, unsigned int rspdata_sz)
{
AnError err = AnCmd_Invoke_S(ctx, dev, api, cmd, cmddata, cmddata_sz, rspdata, rspdata_sz);
if (err == AnError_UNSUPPORTED)
An_LOG(ctx, AnLog_ERROR, "Ensure that device bootloader is active.");
return err;
}
AnError AnFlash_Info_S(AnCtx *ctx, AnDevice *dev, AnFlashInfo *info)
{
uint8_t rsp[6];
AnError err = helpfully_invoke_cmd(ctx, dev,
AnFlash_API, AnFlash_CMD_INFO,
NULL, 0,
&rsp, sizeof rsp);
if (!err) {
info->pagesize = rsp[0] << 8 | rsp[1];
info->numpages = rsp[2] << 24 | rsp[3] << 16 | rsp[4] << 8 | rsp[5];
}
return err;
}
static AnError read_page(AnCtx *ctx, AnDevice *dev,
uint32_t pageidx)
{
uint8_t arg[4] = {pageidx >> 24,
pageidx >> 16 & 0xff,
pageidx >> 8 & 0xff,
pageidx & 0xff};
uint8_t rdst;
AnError err = helpfully_invoke_cmd(ctx, dev,
AnFlash_API, AnFlash_CMD_PAGEREAD,
arg, sizeof arg,
&rdst, 1);
if (err)
return err;
if (rdst) {
An_LOG(ctx, AnLog_ERROR, "Flash API error 0x%02x",
(unsigned int)rdst);
return AnError_CMDFAILURE;
}
return AnError_SUCCESS;
}
static AnError write_page(AnCtx *ctx, AnDevice *dev,
uint32_t pageidx)
{
uint8_t arg[4] = {pageidx >> 24,
pageidx >> 16 & 0xff,
pageidx >> 8 & 0xff,
pageidx & 0xff};
uint8_t rdst;
AnError err = helpfully_invoke_cmd(ctx, dev,
AnFlash_API, AnFlash_CMD_PAGEWRITE,
arg, sizeof arg,
&rdst, 1);
if (err)
return err;
if (rdst) {
An_LOG(ctx, AnLog_ERROR, "Flash API error 0x%02x",
(unsigned int)rdst);
return AnError_CMDFAILURE;
}
return AnError_SUCCESS;
}
static AnError read_buffer(AnCtx *ctx, AnDevice *dev,
uint16_t off, uint8_t len, uint8_t *out)
{
if (len > 48) {
An_LOG(ctx, AnLog_ERROR, "page buffer read is max 48 bytes");
return AnError_OUTOFRANGE;
}
uint8_t arg[3] = {off >> 8, off & 0xff, len};
uint8_t rsp[56];
AnError err = helpfully_invoke_cmd(ctx, dev,
AnFlash_API, AnFlash_CMD_BUFREAD,
arg, sizeof arg,
rsp, sizeof rsp);
if (err)
return err;
if (rsp[0]) {
An_LOG(ctx, AnLog_ERROR, "Flash API error 0x%02x",
(unsigned int)rsp[0]);
return AnError_CMDFAILURE;
}
memcpy(out, rsp + 8, len);
return AnError_SUCCESS;
}
static AnError write_buffer(AnCtx *ctx, AnDevice *dev,
uint16_t off, uint8_t len, const uint8_t *in)
{
if (len > 48) {
An_LOG(ctx, AnLog_ERROR, "page buffer write is max 48 bytes");
return AnError_OUTOFRANGE;
}
uint8_t arg[56] = {[0] = off >> 8, [1] = off & 0xff, [2] = len};
memcpy(arg + 8, in, len);
uint8_t rspst;
AnError err = helpfully_invoke_cmd(ctx, dev,
AnFlash_API, AnFlash_CMD_BUFWRITE,
arg, sizeof arg,
&rspst, 1);
if (err)
return err;
if (rspst) {
An_LOG(ctx, AnLog_ERROR, "Flash API error 0x%02x",
(unsigned int)rspst);
return AnError_CMDFAILURE;
}
return AnError_SUCCESS;
}
#define min(a, b) ((a) < (b) ? (a) : (b))
AnError AnFlash_ReadPage_S(AnCtx *ctx, AnDevice *dev,
AnFlashInfo *info,
uint32_t pageidx, uint8_t *page)
{
AnError err;
/* Load page to page buffer. */
err = read_page(ctx, dev, pageidx);
if (err)
return err;
/* Read out page buffer. */
for (uint16_t off = 0; off < info->pagesize;) {
uint8_t len = min(48, info->pagesize - off);
err = read_buffer(ctx, dev, off, len, page);
if (err)
return err;
off += len;
page += len;
}
return AnError_SUCCESS;
}
AnError AnFlash_WritePage_S(AnCtx *ctx, AnDevice *dev,
AnFlashInfo *info,
uint32_t pageidx, const uint8_t *page)
{
AnError err;
/* Load page buffer. */
for (uint16_t off = 0; off < info->pagesize;) {
uint8_t len = min(48, info->pagesize - off);
err = write_buffer(ctx, dev, off, len, page);
if (err)
return err;
off += len;
page += len;
}
/* Write page buffer to page. */
err = write_page(ctx, dev, pageidx);
if (err)
return err;
return AnError_SUCCESS;
}