Skip to content

Commit 203eabc

Browse files
authored
don't let memory peeks progress flash command reads (#546)
1 parent 7f94a92 commit 203eabc

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

core/flash.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,30 +163,52 @@ static void flash_execute_command(void) {
163163
}
164164
}
165165
static uint8_t flash_read_command(bool peek) {
166-
(void)peek;
166+
uint8_t value = 0;
167+
uint8_t addr = flash.commandAddress;
167168
switch (flash.command[0xF]) {
168169
case 0x05: // Read Status Register-1
169-
return flash.commandStatus[1];
170+
value = flash.commandStatus[1];
171+
break;
170172
case 0x35: // Read Status Register-2
171-
return flash.commandStatus[2];
173+
value = flash.commandStatus[2];
174+
break;
172175
case 0x15: // Read Status Register-3
173-
return flash.commandStatus[3];
176+
value = flash.commandStatus[3];
177+
break;
174178
case 0xAB: // Release Power-down / Device ID
175-
flash.commandAddress = 1;
179+
if (!peek) {
180+
flash.commandAddress = 1;
181+
}
182+
addr = 1;
176183
// fallthrough
177184
case 0x90: // Read Manufacturer / Device ID
178185
case 0x92: // Read Manufacturer / Device ID Dual I/O
179186
case 0x94: // Read Manufacturer / Device ID Quad I/O
180-
return 0xEF15 >> (~flash.commandAddress++ & 1) * 8;
187+
value = 0xEF15 >> (~addr & 1) * 8;
188+
if (!peek) {
189+
flash.commandAddress++;
190+
}
191+
break;
181192
case 0x4B: // Read Unique ID Number
182-
return flash.uniqueID >> (~flash.commandAddress++ & 7) * 8;
193+
value = flash.uniqueID >> (~addr & 7) * 8;
194+
if (!peek) {
195+
flash.commandAddress++;
196+
}
197+
break;
183198
case 0x9F: // Read JEDEC ID
184-
if (flash.commandAddress >= 3) {
185-
flash.commandAddress = 0;
199+
if (addr >= 3) {
200+
if (!peek) {
201+
flash.commandAddress = 0;
202+
}
203+
addr = 0;
186204
}
187-
return 0xEF4016 >> (2 - flash.commandAddress++) * 8;
205+
value = 0xEF4016 >> (2 - addr) * 8;
206+
if (!peek) {
207+
flash.commandAddress++;
208+
}
209+
break;
188210
}
189-
return 0;
211+
return value;
190212
}
191213
static void flash_write_command(uint8_t byte) {
192214
switch (flash.command[0xF]) {

0 commit comments

Comments
 (0)