diff --git a/Source/Teensy/DriveDirLoad.ino b/Source/Teensy/DriveDirLoad.ino index d3e7be7e5..c611af9bc 100644 --- a/Source/Teensy/DriveDirLoad.ino +++ b/Source/Teensy/DriveDirLoad.ino @@ -95,18 +95,6 @@ FLASHMEM void MarkVmClaimed() VmClaimedItems[Num/8] |= 1 << Num%8; } -// Whether a card is in the slot, from card detect on DAT3. CheckLaunchSDAuto's check -// (Teensy.ino), copied rather than shared so that boot stayed untouched for the 0.9 -// release. It takes the pin back first, so it reads true whoever had it last, a failed -// SD.begin included. -FLASHMEM bool SDSlotFilled() -{ - // _SD_DAT3 = pin 46 - pinMode(46, INPUT_PULLDOWN); - delayMicroseconds(5); //let the pulldown settle before reading; see PaulStoffregen/SD@c535ae9 - return digitalReadFast(46); -} - // rsShowExtPackages (StatusFunctions.c), for the Installed Extension page. Here rather // than beside the other status functions because that file is included ahead of the // registry. Reads the card whatever the menu's source is. @@ -131,7 +119,7 @@ FLASHMEM void ShowVmPackages() //Begun here, with one retry while the slot still reads filled: the retry //SDFullInit makes, because a card's first start can fail. Nothing is remembered //between draws, so a card that will not start costs both attempts on each one. - Card = SD.begin(BUILTIN_SDCARD) || (SDSlotFilled() && SD.begin(BUILTIN_SDCARD)); + Card = SDBegin() || (SDSlotFilled() && SDBegin()); //A card started here in place of one that had been begun: whatever the menu holds //from that one is stale, its listing and the extension table built from it. The //flags remote file commands raise for the same reason make the menu's next diff --git a/Source/Teensy/MinimalBoot/Common/VMPackageList.h b/Source/Teensy/MinimalBoot/Common/VMPackageList.h index f2a75604e..173dc13f4 100644 --- a/Source/Teensy/MinimalBoot/Common/VMPackageList.h +++ b/Source/Teensy/MinimalBoot/Common/VMPackageList.h @@ -14,19 +14,10 @@ // The page has rows 12-20 for it, 21 being the divider: nine rows. A row stays within // 39 columns, because one that fills all 40 makes the screen editor advance by itself // and the next message's leading return then leaves a blank row. -// -// COPIES: the walk over /VMS in collect() repeats VmRegistry::find() and refresh() -// (VMRegistry.h) -- the 32-folder limit, the id rules, the manifest parse -- and -// fileCheck() repeats preflight()'s module-header and client-descriptor checks without -// its two CRC passes. They are copies rather than shared so that this page left the -// launch path untouched for the 0.9 release. A change to those rules there has to be -// made here too, or this page and the launch will disagree. namespace VmPackageList { -enum : unsigned { Rows=9, Columns=39, PackageRows=8, IdColumns=10, ExtensionColumns=26 }; +enum : unsigned { Rows=9, Columns=39, ReservedNotes=3, IdColumns=10, ExtensionColumns=26 }; typedef char Row[Columns+1]; -enum : unsigned { FolderLimit=32 }; // find()'s and refresh()'s -enum Walk : uint8_t { WalkDone, WalkNoDirectory, WalkOverLimit, WalkError }; enum State : uint8_t { Listed, BadName, BadManifest }; // What launching would refuse, in the order tryLaunch() finds it. enum Refusal : uint8_t { NoRefusal, ModuleFile, ClientFile, Services, CodeBase }; @@ -36,38 +27,25 @@ struct Entry { State state; Refusal refusal; uint32_t detail; // Services: the bits the host lacks; CodeBase: the module's base }; -struct Scan { Entry entry[FolderLimit]; unsigned count; Walk walk; bool cardRead; }; +struct Scan { Entry entry[VmRegistry::FolderLimit]; unsigned count; VmRegistry::Walk walk; bool cardRead; }; // A byte of a folder name goes to the screen only if an id could hold it. Anything // else becomes '?', so the row still says which folder it is without handing the C64 // a control code. static FLASHMEM char shown(char c){const char s[2]={c,0};return vm_path_component(s)?c:'?';} -// preflight()'s checks on the module image's header and the client cartridge's -// descriptor, with the same conditions, and without the CRC passes over every byte of -// both files that follow them there. A copy: see the top of this file. -static FLASHMEM Refusal fileCheck(const char *root,const VmManifest &m,VmImageHeader &h){ - char path[128];snprintf(path,sizeof path,"%s/%s",root,m.module);FsFile f=SD.sdfs.open(path,O_RDONLY); - const bool module=f&&!f.isDirectory()&&f.fileSize()<=UINT32_MAX&&f.read(&h,64)==64&&vm_valid_header(h,f.fileSize()); - f.close();if(!module)return ModuleFile; - snprintf(path,sizeof path,"%s/%s",root,m.client);f=SD.sdfs.open(path,O_RDONLY);uint8_t d[128]; - const bool client=f&&f.fileSize()==0x6070&&f.seekSet(0x4070)&&f.read(d,128)==128&&!memcmp(d,"VMH1",4)&&d[4]==VM_ABI&& - memchr(d+16,0,24)&&!strcmp((char *)d+16,m.id)&&vm_crc32(d,124)==*(uint32_t *)(d+124); - f.close();return client?NoRefusal:ClientFile; -} - -// One folder of /VMS. `n` is getName's answer: an id is 1-23 characters of -// vm_path_component's set, as find() and refresh() require. +// One folder of /VMS, `n` being getName's answer. static FLASHMEM void entry(Scan &s,const VmHostId *host,const char *name,size_t n){ Entry &e=s.entry[s.count++]; memset(&e,0,sizeof e); for(size_t i=0;name[i]&&i+1=sizeof(VmManifest::id)||!VmRegistry::component(name)){e.state=BadName;return;} + if(!VmRegistry::folderIsId(name,n)){e.state=BadName;return;} char root[80];snprintf(root,sizeof root,"/VMS/%s",name);VmManifest m{}; if(!VmRegistry::readManifest(root,m)){e.state=BadManifest;return;} e.state=Listed;strcpy(e.extension,m.extension); VmImageHeader h{}; - if((e.refusal=fileCheck(root,m,h))!=NoRefusal)return; + const VmRegistry::Fault fault=VmRegistry::verify(root,m,h,VmRegistry::Quick); + if(fault!=VmRegistry::Sound){e.refusal=fault==VmRegistry::ModuleBad?ModuleFile:ClientFile;return;} // What the host is owed, only when it can say: one without a descriptor is // launched without these checks, and one on another ABI refuses every package, // which layout() says once. @@ -81,34 +59,24 @@ static FLASHMEM void entry(Scan &s,const VmHostId *host,const char *name,size_t // card itself (ShowVmPackages, DriveDirLoad.ino). It cannot be found here by reading: // with the card pulled, SdFat still opens the root directory it remembers, so a missing // card read as one without /VMS. +struct Collecting { Scan &scan; const VmHostId *host; }; +static FLASHMEM void collected(void *context,const char *name,size_t n){Collecting &c=*(Collecting *)context;entry(c.scan,c.host,name,n);} static FLASHMEM void collect(Scan &s,const VmHostId *host,bool card){ - s.count=0;s.cardRead=card;s.walk=WalkNoDirectory; - FsFile directory; - if(card&&(directory=SD.sdfs.open("/VMS",O_RDONLY))){ - s.walk=WalkDone;unsigned scanned=0;FsFile item; - while(item.openNext(&directory,O_RDONLY)){ - if(item.isDirectory()){ - if(++scanned>FolderLimit){item.close();s.walk=WalkOverLimit;break;} - // Wider than any id, so a folder that is not one can still be named. - // SdFat gives back an empty name when it does not fit. - char name[64]{};const size_t n=item.getName(name,sizeof name);name[sizeof name-1]=0; - entry(s,host,name,n); - } - item.close(); - } - if(s.walk==WalkDone&&directory.getError())s.walk=WalkError; - directory.close(); - } + s.count=0;s.cardRead=card;s.walk=VmRegistry::WalkNoDirectory; + Collecting c{s,host}; + if(card)s.walk=VmRegistry::walk(collected,&c); // By id, ignoring case: the card lists folders in the order they were made. for(unsigned i=1;i0;j--){const Entry t=s.entry[j];s.entry[j]=s.entry[j-1];s.entry[j-1]=t;} } -struct Out { Row *row; unsigned count; }; -// One row, if the block has room for it. A row too wide is cut at the last column with -// a '>', as MakeExtHostStr marks its own cut, rather than silently. +struct Out { Row *row; unsigned count; unsigned wanted; }; +// One row, if the block has room for it; `wanted` counts the ones that did not fit. A row +// too wide is cut at the last column with a '>', as MakeExtHostStr marks its own cut, +// rather than silently. __attribute__((format(printf,2,3))) static FLASHMEM void add(Out &out,const char *format,...){ + out.wanted++; if(out.count==Rows)return; char line[96];va_list args;va_start(args,format);vsnprintf(line,sizeof line,format,args);va_end(args); if(strlen(line)>Columns){line[Columns-1]='>';line[Columns]=0;} @@ -153,28 +121,26 @@ static FLASHMEM void packageRow(Out &out,const Scan &s,unsigned i){ } // Fills `rows` and returns how many: the packages, then what would refuse a launch, then -// the last launch, each only while there is a row left for it. `last` is the record the -// main image collected at boot (VmFail::captured), null when there was none. +// the last launch. When they do not all fit, the packages give way first, down to the +// rows ReservedNotes holds back, and a "+N more" row stands in for each part cut. `last` +// is the record the main image collected at boot (VmFail::captured), null when there +// was none. static FLASHMEM unsigned layout(const Scan &s,bool installed,const VmHostId *host,const VmFailRecord *last,Row (&rows)[Rows]){ - Out out{rows,0}; + Row reasonRows[Rows],lastRow[1]; + Out reasons{reasonRows,0,0},message{lastRow,0,0}; unsigned listed=0;for(unsigned i=0;iPackageRows?PackageRows-1:s.count; - for(unsigned i=0;iabi!=VM_ABI)add(out," Host is ABI %lu, firmware needs %lu.",(unsigned long)host->abi,(unsigned long)VM_ABI); + if(!installed)add(reasons," No host installed, so none launch."); + else if(host&&host->abi!=VM_ABI)add(reasons," Host is ABI %lu, firmware needs %lu.",(unsigned long)host->abi,(unsigned long)VM_ABI); } // An extension more than one package claims, once, at the first of them. @@ -184,8 +150,8 @@ static FLASHMEM unsigned layout(const Scan &s,bool installed,const VmHostId *hos bool earlier=false;unsigned others=0,other=0; for(unsigned j=0;jcode_floor)add(out," !%s: host takes code at $%lx only",e.id,(unsigned long)VM_CODE_BASE); - else add(out," !%s: code $%lx below host $%lx",e.id,(unsigned long)e.detail, + if(!host||!host->code_floor)add(reasons," !%s: host takes code at $%lx only",e.id,(unsigned long)VM_CODE_BASE); + else add(reasons," !%s: code $%lx below host $%lx",e.id,(unsigned long)e.detail, (unsigned long)vm_host_code_floor(host->code_floor)); break; case NoRefusal:break; @@ -213,9 +179,23 @@ static FLASHMEM unsigned layout(const Scan &s,bool installed,const VmHostId *hos const uint8_t code=(uint8_t)last->code; const char *text=code==VmFail::Ok?"extension started":code==VmFail::Exited?"extension exited": code==VmFail::Faulted?"extension crashed":VmFail::describe(code); - if(last->detail&&code!=VmFail::Installed)add(out," Msg: %s ($%lx)",text,(unsigned long)last->detail); - else add(out," Msg: %s",text); + if(last->detail&&code!=VmFail::Installed)add(message," Msg: %s ($%lx)",text,(unsigned long)last->detail); + else add(message," Msg: %s",text); } + + const unsigned needed=reasons.wanted+message.count; + const unsigned reserved=neededFolderLimit){item.close();directory.close();return WalkOverLimit;} + char name[64]{};const size_t n=item.getName(name,sizeof name);name[sizeof name-1]=0; + visit(context,name,n); + } + item.close(); + } + const bool error=directory.getError();directory.close();return error?WalkError:WalkDone; +} // find() returns how many packages matched (0 or 1), or one of these. On // Ambiguous, `rival` (if given) holds the first claimant's id and how many claim // it; launch.root is the last claimant's. enum : int { OverLimitOrError=-1, Ambiguous=-2 }; struct Rival { char id[sizeof(Manifest::id)]; unsigned claims; }; -static FLASHMEM int find(const char *extension,const char *clientId,Launch &launch,Rival *rival=nullptr){ - FsFile directory=SD.sdfs.open("/VMS",O_RDONLY);if(!directory)return 0; - unsigned scanned=0,found=0;FsFile item; - while(item.openNext(&directory,O_RDONLY)){ - if(item.isDirectory()){ - char name[sizeof(Manifest::id)+1],root[80];const size_t n=item.getName(name,sizeof name); //+1 so n32){item.close();directory.close();return OverLimitOrError;} - if(n&&nid,strrchr(launch.root,'/')+1);strcpy(launch.root,root);launch.manifest_crc=m.crc; - } - } - } - item.close(); +struct Search { const char *extension; const char *clientId; Launch &launch; Rival *rival; unsigned found; }; +static FLASHMEM void claimant(void *context,const char *name,size_t n){ + Search &s=*(Search *)context;if(!folderIsId(name,n))return; + char root[80];snprintf(root,sizeof root,"/VMS/%s",name);Manifest m{}; + if(readManifest(root,m)&&((s.clientId&&strcmp(s.clientId,m.id)==0)||(!s.clientId&&extensionMatches(m.extension,s.extension)))){ + if(s.found++==1&&s.rival)strcpy(s.rival->id,strrchr(s.launch.root,'/')+1);strcpy(s.launch.root,root);s.launch.manifest_crc=m.crc; } +} +static FLASHMEM int find(const char *extension,const char *clientId,Launch &launch,Rival *rival=nullptr){ + Search search{extension,clientId,launch,rival,0}; + const Walk w=walk(claimant,&search);const unsigned found=search.found; + if(w==WalkNoDirectory)return 0; + if(w==WalkOverLimit||w==WalkError)return OverLimitOrError; if(rival)rival->claims=found; - const bool error=directory.getError();directory.close();return error?OverLimitOrError:found>1?Ambiguous:(int)found; + return found>1?Ambiguous:(int)found; } static FLASHMEM bool consume(Launch &l){ FsFile f=SD.sdfs.open("/VMS/launch.vml",O_RDONLY); @@ -54,23 +74,33 @@ static FLASHMEM bool consume(Launch &l){ return ok&&vm_launch_valid(l); } #ifndef MinimumBuild -static FLASHMEM bool preflight(const Launch &l,VmImageHeader *out=nullptr){ - Manifest m{};if(!readManifest(l.root,m)||m.crc!=l.manifest_crc)return false; - char path[128];snprintf(path,sizeof path,"%s/%s",l.root,m.module);FsFile f=SD.sdfs.open(path,O_RDONLY);VmImageHeader h{}; - if(!f||f.isDirectory()||f.fileSize()>UINT32_MAX||f.read(&h,64)!=64||!vm_valid_header(h,f.fileSize())){f.close();return false;} - if(out)*out=h; - uint8_t block[512];uint32_t remain=vm_image_payload_bytes(h),c=~0u; - while(remain){const unsigned n=remain>sizeof block?sizeof block:remain;if(f.read(block,n)!=(int)n){f.close();return false;} +enum Fault : uint8_t { Sound, ModuleBad, ClientBad }; +// Quick leaves out the payload and client bank CRCs, which read every byte of both files. +enum Depth : uint8_t { Quick, Full }; +// Whether the package at `root` has a module image and a client cartridge that launch +// would accept. `h` is the module's header once it reads. +static FLASHMEM Fault verify(const char *root,const Manifest &m,VmImageHeader &h,Depth depth=Full){ + char path[128];snprintf(path,sizeof path,"%s/%s",root,m.module);FsFile f=SD.sdfs.open(path,O_RDONLY); + if(!f||f.isDirectory()||f.fileSize()>UINT32_MAX||f.read(&h,64)!=64||!vm_valid_header(h,f.fileSize())){f.close();return ModuleBad;} + uint8_t block[512];uint32_t remain=depth==Full?vm_image_payload_bytes(h):0,c=~0u; + while(remain){const unsigned n=remain>sizeof block?sizeof block:remain;if(f.read(block,n)!=(int)n){f.close();return ModuleBad;} for(unsigned i=0;i>1)^((0u-(c&1))&0xedb88320u);}remain-=n;} - f.close();if(~c!=h.payload_crc)return false; - snprintf(path,sizeof path,"%s/%s",l.root,m.client);f=SD.sdfs.open(path,O_RDONLY);uint8_t d[128]; + f.close();if(depth==Full&&~c!=h.payload_crc)return ModuleBad; + snprintf(path,sizeof path,"%s/%s",root,m.client);f=SD.sdfs.open(path,O_RDONLY);uint8_t d[128]; if(!f||f.fileSize()!=0x6070||!f.seekSet(0x4070)||f.read(d,128)!=128||memcmp(d,"VMH1",4)||d[4]!=VM_ABI|| - !memchr(d+16,0,24)||strcmp((char *)d+16,m.id)||vm_crc32(d,124)!=*(uint32_t *)(d+124)){f.close();return false;} + !memchr(d+16,0,24)||strcmp((char *)d+16,m.id)||vm_crc32(d,124)!=*(uint32_t *)(d+124)){f.close();return ClientBad;} + if(depth==Quick){f.close();return Sound;} c=~0u;for(unsigned bank=0;bank<2;bank++){ - if(!f.seekSet(64+16+bank*8208)){f.close();return false;} - for(unsigned offset=0;offset<8192;offset+=512){if(f.read(block,512)!=512){f.close();return false;} + if(!f.seekSet(64+16+bank*8208)){f.close();return ClientBad;} + for(unsigned offset=0;offset<8192;offset+=512){if(f.read(block,512)!=512){f.close();return ClientBad;} for(auto v:block){c^=v;for(unsigned b=0;b<8;b++)c=(c>>1)^((0u-(c&1))&0xedb88320u);}} - }f.close();return ~c==*(uint32_t *)(d+8); + }f.close();return ~c==*(uint32_t *)(d+8)?Sound:ClientBad; +} +static FLASHMEM bool preflight(const Launch &l,VmImageHeader *out=nullptr){ + Manifest m{};if(!readManifest(l.root,m)||m.crc!=l.manifest_crc)return false; + VmImageHeader h{};const Fault fault=verify(l.root,m,h); + if(out)*out=h; + return fault==Sound; } // The extensions the installed packages claim, so launching an ordinary file // need not rescan /VMS. Rebuilt wherever the SD listing is, which keeps the @@ -83,21 +113,15 @@ static FLASHMEM bool preflight(const Launch &l,VmImageHeader *out=nullptr){ // caller falls back to the scan. enum Association : uint8_t { NotAssociated, Associated, Unknown }; static char extensions[32][sizeof(Manifest::extension)];static uint8_t extensionCount;static bool extensionsKnown; +static FLASHMEM void recordExtension(void *,const char *name,size_t n){ + if(!folderIsId(name,n))return; + char root[80];Manifest m{};snprintf(root,sizeof root,"/VMS/%s",name); + if(readManifest(root,m))strcpy(extensions[extensionCount++],m.extension); +} static FLASHMEM void refresh(bool sd){ extensionCount=0;extensionsKnown=false;if(!sd)return; - FsFile dir=SD.sdfs.open("/VMS",O_RDONLY);if(!dir)return; - FsFile item;unsigned scanned=0;bool overflowed=false; - while(item.openNext(&dir,O_RDONLY)){ - if(item.isDirectory()){ - if(++scanned>32){overflowed=true;item.close();break;} - char id[sizeof(Manifest::id)+1],root[80];auto n=item.getName(id,sizeof id);Manifest m{}; //+1 as in find() - if(n&&n diff --git a/vm/abi/README.md b/vm/abi/README.md index b856e0a3b..c40d22b55 100644 --- a/vm/abi/README.md +++ b/vm/abi/README.md @@ -252,7 +252,7 @@ than guessing. A shared extension refuses only files of that extension, and the refusal names both packages, or how many there are and two of them. The C64 settings menu's Installed Extension page (`F8`, `0`) lists what that scan -sees, by the same rules (a copy of them in `VMPackageList.h`): each package's extensions, a +sees, by the same code (`VmRegistry` in `VMRegistry.h`, which `VMPackageList.h` calls): each package's extensions, a directory skipped for its name or its manifest, a shared extension, and what the installed host would refuse, so these can be found before a launch rather than by one. diff --git a/vm/tests/packages_test.cpp b/vm/tests/packages_test.cpp index 51eb19879..0e4dd6ed1 100644 --- a/vm/tests/packages_test.cpp +++ b/vm/tests/packages_test.cpp @@ -129,17 +129,33 @@ int main(int argc,char **argv){ {const Lines got=rows(nullptr);assert(got[0].substr(2,5)=="alpha"&&got[1].substr(2,4)=="Beta");} fs::remove_all(base/"VMS/alpha");fs::remove_all(base/"VMS/Beta"); - // More packages than rows: seven, then a count of the rest, and still a row for why. + // More packages than rows: five, a count of the rest, then as many reasons as are left. + // The packages give way, never the reasons or the last launch: what does not fit is + // counted on a row of its own. for(unsigned i=0;i<6;i++)manifest("PKG"+std::to_string(i),"p"+std::to_string(i)); {const Lines got=rows(&host); - assert(got.size()==Rows&&got[6]==" !PKG4 p4"&&got[7]==" +3 more"); - assert(got[8]==" !PKG0: module file bad or missing");} + assert(got.size()==Rows&&got[4]==" !PKG2 p2"&&got[5]==" +5 more"); + assert(got[6]==" !PKG0: module file bad or missing"&&got[7]==" !PKG1: module file bad or missing"); + assert(got[8]==" +6 more notes");} + {const VmFailRecord crashed=record(VmFail::Faulted,0); + const Lines got=rows(&host,true,&crashed); + assert(got.size()==Rows&&got[5]==" +5 more"&&got[6]==" !PKG0: module file bad or missing"&& + got[7]==" +7 more notes"&&got[8]==" Msg: extension crashed");} for(unsigned i=0;i<6;i++)fs::remove_all(base/"VMS"/("PKG"+std::to_string(i))); + // The list reads headers only, so a payload that fails its CRC still lists clean; + // launch (preflight) is where it is refused. + {const fs::path module=base/"VMS/HELLO/engine.mvm"; + const fs::path kept=base/"engine.keep";fs::copy_file(module,kept); + {std::fstream io(module,std::ios::binary|std::ios::in|std::ios::out); + io.seekg(-1,std::ios::end);char c=0;io.get(c);io.seekp(-1,std::ios::end);io.put(char(c^1));} + assert(has(rows(nullptr)," HELLO hi")); + fs::copy_file(kept,module,fs::copy_options::overwrite_existing);fs::remove(kept);} + // Over the limit, every launch is refused; the list says so after what it read. - for(unsigned i=0;i