/* Included by main.c after ram is defined. Durable file device; guest owns shell. */ enum { SNAP_WORDS=4312, SNAP_WIDE_WORDS=8408, SNAP_META_WORDS=216, SNAP_OLD_FILE_WORDS=4096, SNAP_FILE_WORDS=8192 }; static uint32_t kernelIdentity; static uint32_t snapshotHash(const uint32_t *words,unsigned count) { uint32_t hash=2166136261u; for(unsigned i=0;i>8)&255))*16777619u; if(cpu32) {hash=(hash^((words[i]>>16)&255))*16777619u;hash=(hash^(words[i]>>24))*16777619u;} } return hash; } /* Layout 0: fixed 512-word slots; 1: packed 4096-word B32S v2; 2: packed v3. */ static int snapshotValidFormat(const uint32_t *words,int layout) { unsigned capacity=layout==2?SNAP_FILE_WORDS:SNAP_OLD_FILE_WORDS; unsigned packedOffset=0;int packed=layout!=0; for(unsigned i=0;i<8;i++) { unsigned length=words[192+i],kind=words[200+i],used=words[208+i]; if(used>1 || kind>(cpu32?(packed?3u:2u):1u) || length>=(packed?capacity:512u))return 0; if(!used)continue; const uint32_t *name=words+i*24;unsigned n=0; while(n<24&&name[n]) {if(name[n]<33||name[n]>126)return 0;n++;} unsigned contentOffset=packed?packedOffset:i*512; if(!n||n==24||contentOffset+length>=capacity||words[SNAP_META_WORDS+contentOffset+length])return 0; if(!kind)for(unsigned k=0;k255)return 0; if(packed)packedOffset+=length+1; for(unsigned j=0;j0xf000-4)return -3; unsigned addresses[4],sizes[4]={192,8,8,8}; for(unsigned i=0;i<4;i++) { addresses[i]=cpu32?(uint32_t)memoryRead(descriptor+i):(uint16_t)ram.memory[descriptor+i]; uint64_t end=(uint64_t)addresses[i]+sizes[i]; int lowArray=addresses[i]>=0x300&&end<=(cpu32?0xc000u:0xa000u); uint32_t kernelStart=bootMode?bootOrigin:(assembledWide?assembledOrigin:0); uint64_t kernelEnd=bootMode?(uint64_t)bootOrigin+bootWords:(assembledWide?(uint64_t)assembledOrigin+assembledWords:0); int kernelArray=cpu32&&addresses[i]>=kernelStart&&end<=kernelEnd; if(!lowArray&&!kernelArray)return -3; for(unsigned j=0;j>(8*i));header[12+i]=(unsigned char)(hash>>(8*i));} int okay=fwrite(header,1,16,file)==16; for(unsigned i=0;i>(8*b))&255,file)==EOF)okay=0; if(fclose(file))okay=0; if(okay) { #ifdef _WIN32 okay=MoveFileExA(temporary,path,MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH)!=0; #else okay=rename(temporary,path)==0; #endif } if(!okay)remove(temporary); free(temporary);return okay?0:-1; } if(operation!=1)return -3; FILE *file=fopen(path,"rb");if(!file)return -1; unsigned char header[16]={0};int valid=fread(header,1,16,file)==16,layout=-1; unsigned snapshotWords=0,wordBytes=cpu32?4:2; if(valid) { if(!cpu32&&!memcmp(header,"B16S\1\0\0\0",8)){layout=0;snapshotWords=SNAP_WORDS;} else if(cpu32&&!memcmp(header,"B32S\1\0\0\0",8)){layout=0;snapshotWords=SNAP_WORDS;} else if(cpu32&&!memcmp(header,"B32S\2\0\0\0",8)){layout=1;snapshotWords=SNAP_WORDS;} else if(cpu32&&!memcmp(header,"B32S\3\0\0\0",8)){layout=2;snapshotWords=SNAP_WIDE_WORDS;} else valid=0; } for(unsigned i=0;valid&&i