forked from satiator/satiator-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ar.c
189 lines (150 loc) · 4.91 KB
/
ar.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
#include <satiator.h>
#include <iapetus.h>
#include <string.h>
#include <stdio.h>
#include "gui/gmenu.h"
#define AR_FLASH_ADDR ((volatile u16 *)0x22000000)
static flash_info_struct flash_info;
int flash_inited = 0;
static int init_flash(void) {
if (flash_inited)
return 1;
int ret = ar_init_flash_io(&flash_info);
char msg_buf[64];
uint16_t vid, pid;
if (ret != IAPETUS_ERR_OK) {
switch (ret) {
case IAPETUS_ERR_UNSUPPORTED:
ar_get_product_id(&vid, &pid);
sprintf(msg_buf, "Unsupported Flash ID %04x:%04x", vid, pid);
menu_error("Action Replay", msg_buf);
return 0;
case IAPETUS_ERR_HWNOTFOUND:
menu_error("Action Replay", "No AR cart found!");
return 0;
default:
menu_error("Action Replay", "Unknown error");
return 0;
}
}
flash_inited = 1;
return 1;
}
static void flash_backup_ar(void) {
if (!init_flash())
return;
int fd = s_open("ar_backup.bin", FA_READ|FA_WRITE|FA_CREATE_ALWAYS);
if (fd < 0) {
menu_error("Action Replay", "Could not open ar_backup.bin for writing");
return;
}
int length = flash_info.page_size * flash_info.num_pages;
int done = 0;
uint8_t *ptr = (void*)AR_FLASH_ADDR;
menu_progress_begin("Reading...", length);
while (done < length) {
int to_copy = length;
if (to_copy > S_MAXBUF)
to_copy = S_MAXBUF;
s_write(fd, ptr, to_copy);
done += to_copy;
ptr += to_copy;
menu_progress_update(done);
for (volatile int i=0; i<10000; i++);
}
menu_progress_complete();
s_close(fd);
menu_error("Action Replay", "Successfully backed up to ar_backup.bin.");
}
static void flash_erase_ar(void) {
if (!init_flash())
return;
menu_progress_begin("Erasing...", flash_info.num_pages);
ar_erase_flash_all(&flash_info);
menu_progress_complete();
}
static int bin_file_filter(file_ent *entry) {
if (entry->isdir)
return 1;
int len = strlen(entry->name);
if (!strcasecmp(&entry->name[len-4], ".bin"))
return 1;
return 0;
}
static void flash_flash_ar(void) {
if (!init_flash())
return;
int nents;
file_ent *list = file_list_create(".", &nents, bin_file_filter);
file_list_sort(list, nents);
int entry = menu_picklist(list, nents, "Choose image to flash");
if (entry < 0)
return;
int fd = s_open(list[entry].name, FA_READ);
file_list_free(list, nents);
if (fd < 0) {
menu_error("Action Replay", "Could not open file");
return;
}
int file_size = s_seek(fd, 0, 2);
s_seek(fd, 0, 0);
if (file_size > (2*flash_info.page_size * flash_info.num_pages)) {
menu_error("Action Replay", "Firmware too big for Flash");
return;
}
uint8_t buf[S_MAXBUF], empty[S_MAXBUF];
int write_size = S_MAXBUF;
int blocks = (file_size + write_size - 1) / write_size;
int page_words = write_size / 2;
int pages_per = page_words / flash_info.page_size;
volatile u16 *write_ptr = AR_FLASH_ADDR;
memset(empty, 0xff, sizeof(empty));
menu_progress_begin("Writing...", blocks);
for (int i=0; i<blocks; i++) {
menu_progress_update(i);
s_read(fd, buf, write_size);
int matches = 1;
int empty = 1;
uint16_t *bptr = (void*)buf;
for (volatile uint16_t *rptr = write_ptr; rptr < write_ptr+page_words; rptr++) {
if (*rptr != 0xffff)
empty = 0;
if (*rptr != *bptr++)
matches = 0;
if (!(empty | matches))
break;
}
if (!matches) {
if (flash_info.needs_page_erase && !empty)
ar_erase_flash(&flash_info, write_ptr, pages_per);
ar_write_flash(&flash_info, write_ptr, (void*)buf, pages_per);
char msg_buf[64];
for (int j=0; j<page_words; j++) {
u16 got = write_ptr[j];
u16 want = ((u16*)buf)[j];
if (got != want) {
sprintf(msg_buf, "Verify error at address 0x%x: %04x!=%04x", j*2, got, want);
menu_error("Action Replay", msg_buf);
return;
}
}
}
write_ptr += page_words;
}
menu_progress_complete();
menu_error("Action Replay", "Flashing complete");
}
const file_ent ar_menu_options[] = {
{"Backup AR", 0, &flash_backup_ar},
{"Erase AR", 0, &flash_erase_ar},
{"Flash AR", 1, &flash_flash_ar},
};
void ar_menu(void) {
while (1) {
int entry = menu_picklist(ar_menu_options, sizeof(ar_menu_options)/sizeof(*ar_menu_options), "Action Replay tools");
if (entry == -1)
return;
void (*submenu)(void) = ar_menu_options[entry].priv;
submenu();
}
}