-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathupdate.c
177 lines (152 loc) · 5.47 KB
/
update.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
#include <stdlib.h>
#include "update.h"
#define ERASEBLOCKNB(ofs) ((ofs)/ERASEBLOCKSIZE)
/**
* Add an update (command) to a struct updates (see update.h for the data
* format)
*
* Returns non-zero in case of error
*/
static int
insert_cmd(struct updates *updates, struct update update) {
struct update *u;
if ((u = malloc(sizeof(*u))) == NULL)
return 1;
*u = update;
updates_insert_tail(updates, u);
return 0;
}
#define insert_writef(u, r) \
insert_cmd(u, (struct update){.cmd = UPDATE_CMD_WRITEF, .rom = (r)})
#define insert_move(u, r) \
insert_cmd(u, (struct update){.cmd = UPDATE_CMD_MOVE, .rom = (r)})
#define insert_write(u, r, s) \
insert_cmd(u, (struct update){.cmd = UPDATE_CMD_WRITE, .rom = (r), \
.update_write_srcslot = (s)})
#define insert_read(u, r, s) \
insert_cmd(u, (struct update){.cmd = UPDATE_CMD_READ, .rom = (r), \
.update_read_dstslot = (s)})
#define insert_erase(u, o) \
insert_cmd(u, (struct update){.cmd = UPDATE_CMD_ERASE, .rom = NULL, \
.update_erase_dstofs = (o)})
static int
update_bigrom(struct updates *updates, struct rom *rom) {
int r;
if (rom->source.type == ROM_SOURCE_FILE) {
if ((r = insert_writef(updates, rom)) != 0)
return r;
} else {
if ((r = insert_move(updates, rom)) != 0)
return r;
}
return 0;
}
static int
update_smallroms(struct updates *updates, struct rom *from) {
struct rom *cur;
int r, slot;
#define FOREACH_SMALLROM(from, cur) \
for ((cur) = (from); \
(cur) != NULL && \
ERASEBLOCKNB((cur)->offset) == ERASEBLOCKNB((from)->offset); \
(cur) = image_next(cur))
/*
* Save in memory the ROMs present in the erase-block: the moved ROMs
* originating from the same erase block and the untouched ROMs.
*/
slot = 0;
FOREACH_SMALLROM(from, cur) {
if (cur->source.type == ROM_SOURCE_FLASH &&
ERASEBLOCKNB(cur->source.u.origoffset) == ERASEBLOCKNB(from->offset)) {
if ((r = insert_read(updates, cur, slot++)) != 0)
return r;
}
}
/* Erase the erase-block explicitly if necessary */
if (from->offset%ERASEBLOCKSIZE != 0) {
r = insert_erase(updates, from->offset - from->offset%ERASEBLOCKSIZE);
if (r != 0)
return r;
}
/*
* Flash the ROMs saved previously, the new ROMs and the moved ROMs
* originating from another erase-block.
*/
slot = 0;
FOREACH_SMALLROM(from, cur) {
if (cur->source.u.origoffset != -1 &&
ERASEBLOCKNB(cur->source.u.origoffset) == ERASEBLOCKNB(from->offset)) {
if ((r = insert_write(updates, cur, slot++)) != 0)
return r;
} else {
if (cur->source.type == ROM_SOURCE_FILE) {
if ((r = insert_writef(updates, cur)) != 0)
return r;
} else {
if ((r = insert_move(updates, cur)) != 0)
return r;
}
}
}
return 0;
}
/**
* Generate I/O commands to be applied to the original image to obtain the
* new image.
*
* image_defrag() in insert.c guarantees that ROMs are always moved from higher
* addresses to lower addresses. As we generate update commands from lower
* addresses to higher addresses, we can be sure that the source ROM of a move
* command cannot be overwritten by a previous command. This keeps us from doing
* a topological sorting.
*
* image must be valid (see image.h) and the ROMs must have a size power of two.
*
* Returns non-zero in case of error.
*/
int
image_update(struct image *image, struct updates **updates) {
struct rom *rom;
int r;
if ((*updates = malloc(sizeof(**updates))) == NULL)
return 1;
updates_init(*updates);
/* For each ROM to be flashed (new ROM or moved ROM): */
image_foreach(image, rom) {
if (rom->source.type == ROM_SOURCE_FLASH &&
rom->offset == rom->source.u.origoffset)
continue;
if (rom->romsize >= ERASEBLOCKSIZE) {
/*
* ROM >= 128 KB (erase-block size): there is no precaution to take,
* the destination erase-blocks can be overwritten.
*/
if ((r = update_bigrom(*updates, rom)) != 0)
return r;
} else {
/*
* ROM < 128 KB (small ROMs): the destination erase-block may
* contain other ROMs. We must preserve the existing ROMs in memory
* before the erasure of the erase-block.
*/
struct rom *prev, *next, *from;
/* Compute from = first ROM of the destination erase-block */
from = rom;
while ((prev = image_prev(from)) != NULL &&
ERASEBLOCKNB(prev->offset) == ERASEBLOCKNB(from->offset)) {
from = prev;
}
if ((r = update_smallroms(*updates, from)))
return r;
/*
* Compute next = last ROM of the erase-block, so the next iteration
* will start at the next erase-block
*/
while ((next = image_next(rom)) != NULL &&
ERASEBLOCKNB(next->offset) == ERASEBLOCKNB(from->offset)) {
rom = next;
}
}
}
return 0;
}