Correctly implemented write protection

This commit is contained in:
Luxferre
2022-08-13 11:49:03 +03:00
parent e46d1e9195
commit 79c0001293
2 changed files with 11 additions and 3 deletions
+10 -2
View File
@@ -125,6 +125,9 @@ static struct EquiRAM ram;
/* Also create an alternative view of the same RAM area for direct offset-based access */
static uchar* flatram = (uchar *)&ram;
/* write protection threshold RAM address */
static ushort write_protection_threshold = 0;
/* Error reporting codes */
enum EquiErrors {
SUCCESS=0,
@@ -329,8 +332,11 @@ ushort persistOp(FILE *pfd, ushort maddr, ushort dataLen, ushort blk, uchar isWr
fseek(pfd, ((unsigned long) blk << 10), SEEK_SET); /* blocks are 1K-aligned */
if(isWrite) /* writing to the persistent area from the memory */
proc = (ushort) fwrite(&flatram[maddr], 1, dataLen, pfd);
else /* reading from the persistent area into the memory */
else { /* reading from the persistent area into the memory */
if(maddr < write_protection_threshold)
trapout(RESTRICTED_WRITE_ERROR);
proc = (ushort) fread(&flatram[maddr], 1, dataLen, pfd);
}
if(proc != dataLen)
status = PERSIST_IO_ERROR;
}
@@ -459,6 +465,8 @@ void equi_main_loop() {
case INS_STORE: /* main stack -> mem (word) */
pbuf = popMain();
pbuf2 = popMain();
if(pbuf < write_protection_threshold)
trapout(RESTRICTED_WRITE_ERROR);
flatram[pbuf] = pbuf2 >> 8U;
flatram[pbuf + 1U] = pbuf2 & 255U;
break;
@@ -614,6 +622,7 @@ int main(int argc, char* argv[]) {
ram.cmd_start = (uchar *)&ram.cmdbuf - (uchar *)&ram.stack_size;
ram.cmd_size = CMD_BUF_SIZE;
ram.II = ram.CM = ram.IM = ram.MM = 0; /* reset all flags */
write_protection_threshold = ram.gpd_start; /* setup write protection */
/* process command line params */
if(argc > 1 && argv[1][0] == 'm') /* enter minification mode, don't run the programs */
ram.MM = 1;
@@ -638,7 +647,6 @@ int main(int argc, char* argv[]) {
if(host_islittle) {
ram.stack_size = tobig(ram.stack_size);
ram.clt_start = tobig(ram.clt_start);
ram.gpd_start = tobig(ram.gpd_start);
ram.cmd_start = tobig(ram.cmd_start);
ram.cmd_size = tobig(ram.cmd_size);
}