From 5723e89f10e0989257aa360a490869ea07a94fc9 Mon Sep 17 00:00:00 2001 From: Kelly Fox Date: Tue, 20 Oct 2026 16:29:21 -0500 Subject: [PATCH 1/5] settings: share the /VMS walk and package checks with the Installed Extension page The page copied find(), refresh() and preflight() and had drifted: it skipped the module-payload and client-bank CRC passes, so a package with a bad CRC showed launchable and then failed at launch. VmRegistry now owns the folder rule, the bounded /VMS walk and verify(), and find(), refresh(), preflight() and the page all call them. SDSlotFilled moves to Teensy.ino beside its other caller; SD.begin already muxes DAT3 back, so the pin needs no restore of its own. The page keeps rows for the reasons and the last launch when many packages are listed, and counts any it still has to cut. The docs say what '!' covers. --- Source/Teensy/DriveDirLoad.ino | 12 -- .../Teensy/MinimalBoot/Common/VMPackageList.h | 121 +++++++----------- Source/Teensy/MinimalBoot/Common/VMRegistry.h | 96 ++++++++------ Source/Teensy/Teensy.ino | 18 ++- docs/General_Usage.md | 2 +- docs/TR_Host_Extensions.md | 4 +- vm/abi/README.md | 2 +- vm/tests/packages_test.cpp | 33 ++++- 8 files changed, 151 insertions(+), 137 deletions(-) diff --git a/Source/Teensy/DriveDirLoad.ino b/Source/Teensy/DriveDirLoad.ino index 253a32ee1..0f5f46785 100644 --- a/Source/Teensy/DriveDirLoad.ino +++ b/Source/Teensy/DriveDirLoad.ino @@ -72,18 +72,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. diff --git a/Source/Teensy/MinimalBoot/Common/VMPackageList.h b/Source/Teensy/MinimalBoot/Common/VMPackageList.h index f2a75604e..27d99ef22 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); + 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. @@ -82,33 +60,20 @@ static FLASHMEM void entry(Scan &s,const VmHostId *host,const char *name,size_t // with the card pulled, SdFat still opens the root directory it remembers, so a missing // card read as one without /VMS. 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; + if(card)s.walk=VmRegistry::walk([&](const char *name,size_t n){entry(s,host,name,n);}); // 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 +118,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 +147,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 +176,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=needed static FLASHMEM Walk walk(Visit visit){ + FsFile directory=SD.sdfs.open("/VMS",O_RDONLY);if(!directory)return WalkNoDirectory; + unsigned scanned=0;FsFile item; + while(item.openNext(&directory,O_RDONLY)){ + if(item.isDirectory()){ + if(++scanned>FolderLimit){item.close();directory.close();return WalkOverLimit;} + char name[64]{};const size_t n=item.getName(name,sizeof name);name[sizeof name-1]=0; + visit(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; - } - } + unsigned found=0; + const Walk w=walk([&](const char *name,size_t n){ + if(!folderIsId(name,n))return; + char root[80];snprintf(root,sizeof root,"/VMS/%s",name);Manifest m{}; + if(readManifest(root,m)&&((clientId&&strcmp(clientId,m.id)==0)||(!clientId&&extensionMatches(m.extension,extension)))){ + if(found++==1&&rival)strcpy(rival->id,strrchr(launch.root,'/')+1);strcpy(launch.root,root);launch.manifest_crc=m.crc; } - item.close(); - } + }); + 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 +70,30 @@ 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; +enum Fault : uint8_t { Sound, ModuleBad, ClientBad }; +// Whether the package at `root` has a module image and a client cartridge that launch +// would accept, every CRC included. `h` is the module's header once it reads. +static FLASHMEM Fault verify(const char *root,const Manifest &m,VmImageHeader &h){ + 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=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;} + 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(~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;} 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 @@ -85,19 +108,12 @@ enum Association : uint8_t { NotAssociated, Associated, Unknown }; static char extensions[32][sizeof(Manifest::extension)];static uint8_t extensionCount;static bool extensionsKnown; 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 Date: Tue, 20 Oct 2026 18:30:18 -0500 Subject: [PATCH 2/5] settings: skip the payload CRC passes when drawing Installed Extension The page redrew with a full read of every package's module and client banks. It now checks sizes and headers only; launch still verifies every CRC. --- Source/Teensy/MinimalBoot/Common/VMPackageList.h | 2 +- Source/Teensy/MinimalBoot/Common/VMRegistry.h | 11 +++++++---- docs/General_Usage.md | 2 +- docs/TR_Host_Extensions.md | 2 +- vm/tests/packages_test.cpp | 16 ---------------- 5 files changed, 10 insertions(+), 23 deletions(-) diff --git a/Source/Teensy/MinimalBoot/Common/VMPackageList.h b/Source/Teensy/MinimalBoot/Common/VMPackageList.h index 27d99ef22..6f6941d2d 100644 --- a/Source/Teensy/MinimalBoot/Common/VMPackageList.h +++ b/Source/Teensy/MinimalBoot/Common/VMPackageList.h @@ -44,7 +44,7 @@ static FLASHMEM void entry(Scan &s,const VmHostId *host,const char *name,size_t if(!VmRegistry::readManifest(root,m)){e.state=BadManifest;return;} e.state=Listed;strcpy(e.extension,m.extension); VmImageHeader h{}; - const VmRegistry::Fault fault=VmRegistry::verify(root,m,h); + 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, diff --git a/Source/Teensy/MinimalBoot/Common/VMRegistry.h b/Source/Teensy/MinimalBoot/Common/VMRegistry.h index acc564e18..574cdc947 100644 --- a/Source/Teensy/MinimalBoot/Common/VMRegistry.h +++ b/Source/Teensy/MinimalBoot/Common/VMRegistry.h @@ -71,18 +71,21 @@ static FLASHMEM bool consume(Launch &l){ } #ifndef MinimumBuild 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, every CRC included. `h` is the module's header once it reads. -static FLASHMEM Fault verify(const char *root,const Manifest &m,VmImageHeader &h){ +// 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=vm_image_payload_bytes(h),c=~0u; + 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 ModuleBad; + 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 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 ClientBad;} for(unsigned offset=0;offset<8192;offset+=512){if(f.read(block,512)!=512){f.close();return ClientBad;} diff --git a/docs/General_Usage.md b/docs/General_Usage.md index 1465c76c3..57173a777 100644 --- a/docs/General_Usage.md +++ b/docs/General_Usage.md @@ -232,7 +232,7 @@ * To install: copy the `.TRH` file to SD or USB and select it in the file browser * Firmware updates keep the installed extension * Lists the file types registered on the SD card: one row per registration folder in `/VMS`, with the file types it claims - * `*` another folder claims the same file type, so neither opens it; `x` the folder couldn't be read; `!` it wouldn't launch: its module or client file is damaged or missing (checksums included), or the installed extension can't run it + * `*` another folder claims the same file type, so neither opens it; `x` the folder couldn't be read; `!` it wouldn't launch: its module or client file is damaged or missing, or the installed extension can't run it * Rows below the list say what stops a launch, then how the last launch went. See [TR Host Extensions](TR_Host_Extensions.md#checking-or-removing-the-installed-extension) * `u` Uninstall the extension (`y` to confirm). TeensyROM restarts afterwards * Also use `u` if the page says `None installed; slot not blank.` That means an earlier install or uninstall didn't finish diff --git a/docs/TR_Host_Extensions.md b/docs/TR_Host_Extensions.md index 74a87372c..003052572 100644 --- a/docs/TR_Host_Extensions.md +++ b/docs/TR_Host_Extensions.md @@ -55,7 +55,7 @@ The TR+ flash holds the core TeensyROM firmware and one extension, side by side. * Below that, it lists the registration folders in `/VMS` on the SD card, one per row, with the file types each one claims. A mark in front of a name flags a problem: * `*` Another folder claims one of the same file types. TeensyROM won't open that file type with either of them until one is removed. * `x` TeensyROM couldn't read the folder: its name isn't valid, or its `manifest.vmi` is missing or invalid. It claims nothing. - * `!` TeensyROM wouldn't launch this one: its module or client file is damaged or missing (checksums included), or the installed extension can't run it. A row below the list says why. + * `!` TeensyROM wouldn't launch this one: its module or client file is damaged or missing, or the installed extension can't run it. A row below the list says why. * After the list, rows say what else stops a launch (for example, no extension installed), then how the last launch went. With many packages the list shortens to a `+N more` row so these stay on screen; a `+N more notes` row stands in for any reasons that still don't fit. * The page checks the SD card each time it opens, so it also shows a card that was just inserted or swapped. * Press `u` to uninstall it, then `y` to confirm. TeensyROM restarts afterwards. diff --git a/vm/tests/packages_test.cpp b/vm/tests/packages_test.cpp index b10dca2eb..60209b957 100644 --- a/vm/tests/packages_test.cpp +++ b/vm/tests/packages_test.cpp @@ -143,22 +143,6 @@ int main(int argc,char **argv){ 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))); - // A package whose header is sound but whose payload or client bank fails its CRC - // would be refused at launch, so it gets the '!' too. - {const fs::path module=base/"VMS/HELLO/engine.mvm",client=base/"VMS/HELLO/client.crt"; - const fs::path moduleKept=base/"engine.keep",clientKept=base/"client.keep"; - fs::copy_file(module,moduleKept);fs::copy_file(client,clientKept); - auto flip=[](const fs::path &f,std::streamoff at){std::fstream io(f,std::ios::binary|std::ios::in|std::ios::out); - io.seekg(at);char c=0;io.get(c);io.seekp(at);io.put(char(c^1));}; - flip(module,fs::file_size(module)-1); - assert(has(rows(nullptr)," !HELLO hi")&&has(rows(nullptr)," !HELLO: module file bad or missing")); - fs::copy_file(moduleKept,module,fs::copy_options::overwrite_existing); - flip(client,64+16+100); - assert(has(rows(nullptr)," !HELLO hi")&&has(rows(nullptr)," !HELLO: client file bad or missing")); - fs::copy_file(clientKept,client,fs::copy_options::overwrite_existing); - fs::remove(moduleKept);fs::remove(clientKept); - assert(has(rows(nullptr)," HELLO hi"));} - // Over the limit, every launch is refused; the list says so after what it read. for(unsigned i=0;i Date: Tue, 20 Oct 2026 18:30:57 -0500 Subject: [PATCH 3/5] test: Installed Extension lists a CRC-bad payload clean; launch refuses it --- vm/tests/packages_test.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/vm/tests/packages_test.cpp b/vm/tests/packages_test.cpp index 60209b957..0e4dd6ed1 100644 --- a/vm/tests/packages_test.cpp +++ b/vm/tests/packages_test.cpp @@ -143,6 +143,15 @@ int main(int argc,char **argv){ 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 Date: Mon, 26 Oct 2026 10:01:54 -0500 Subject: [PATCH 4/5] firmware: the /VMS walk and SDSlotFilled stay in flash walk<> was a FLASHMEM template, but the instance refresh() uses landed in ITCM (388 B), with veneers for readManifest and vm_path_component. walk now takes a FLASHMEM visitor function and a context pointer. SDSlotFilled gets back the FLASHMEM it had before it moved to Teensy.ino. --- .../Teensy/MinimalBoot/Common/VMPackageList.h | 5 ++- Source/Teensy/MinimalBoot/Common/VMRegistry.h | 41 +++++++++++-------- Source/Teensy/Teensy.ino | 2 +- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/Source/Teensy/MinimalBoot/Common/VMPackageList.h b/Source/Teensy/MinimalBoot/Common/VMPackageList.h index 6f6941d2d..173dc13f4 100644 --- a/Source/Teensy/MinimalBoot/Common/VMPackageList.h +++ b/Source/Teensy/MinimalBoot/Common/VMPackageList.h @@ -59,9 +59,12 @@ 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=VmRegistry::WalkNoDirectory; - if(card)s.walk=VmRegistry::walk([&](const char *name,size_t n){entry(s,host,name,n);}); + 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;} diff --git a/Source/Teensy/MinimalBoot/Common/VMRegistry.h b/Source/Teensy/MinimalBoot/Common/VMRegistry.h index 574cdc947..b1b0e7d78 100644 --- a/Source/Teensy/MinimalBoot/Common/VMRegistry.h +++ b/Source/Teensy/MinimalBoot/Common/VMRegistry.h @@ -29,17 +29,19 @@ enum Walk : uint8_t { WalkNoDirectory, WalkDone, WalkOverLimit, WalkError }; // A folder of /VMS can hold a package only if its name is an id: 1-23 characters of // component()'s set. `n` is getName's answer. static FLASHMEM bool folderIsId(const char *name,size_t n){return n&&n static FLASHMEM Walk walk(Visit visit){ +// Calls visit(context,name,n) for each folder of /VMS, the name wide enough that a folder +// that is no id can still be shown. Every reader of /VMS goes through here, so they agree +// on where the walk stops. A function pointer, not a template: FLASHMEM on a template +// does not reach every instance, and refresh()'s landed in ITCM. +using Visit = void (*)(void *context,const char *name,size_t n); +static FLASHMEM Walk walk(Visit visit,void *context){ FsFile directory=SD.sdfs.open("/VMS",O_RDONLY);if(!directory)return WalkNoDirectory; unsigned scanned=0;FsFile item; while(item.openNext(&directory,O_RDONLY)){ if(item.isDirectory()){ if(++scanned>FolderLimit){item.close();directory.close();return WalkOverLimit;} char name[64]{};const size_t n=item.getName(name,sizeof name);name[sizeof name-1]=0; - visit(name,n); + visit(context,name,n); } item.close(); } @@ -50,15 +52,17 @@ template static FLASHMEM Walk walk(Visit visit){ // it; launch.root is the last claimant's. enum : int { OverLimitOrError=-1, Ambiguous=-2 }; struct Rival { char id[sizeof(Manifest::id)]; unsigned claims; }; +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){ - unsigned found=0; - const Walk w=walk([&](const char *name,size_t n){ - if(!folderIsId(name,n))return; - char root[80];snprintf(root,sizeof root,"/VMS/%s",name);Manifest m{}; - if(readManifest(root,m)&&((clientId&&strcmp(clientId,m.id)==0)||(!clientId&&extensionMatches(m.extension,extension)))){ - if(found++==1&&rival)strcpy(rival->id,strrchr(launch.root,'/')+1);strcpy(launch.root,root);launch.manifest_crc=m.crc; - } - }); + 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; @@ -109,13 +113,14 @@ 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; - const Walk w=walk([](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); - }); + const Walk w=walk(recordExtension,nullptr); extensionsKnown=w==WalkDone;if(!extensionsKnown)extensionCount=0; } static FLASHMEM Association associated(const char *name){ diff --git a/Source/Teensy/Teensy.ino b/Source/Teensy/Teensy.ino index ca855ec18..46737fe88 100644 --- a/Source/Teensy/Teensy.ino +++ b/Source/Teensy/Teensy.ino @@ -552,7 +552,7 @@ void SetMenu(StructMenuItem* Source, uint16_t NumItems) // The pin is left a pulldown input, as the SD library itself leaves it when a card is // gone; SD.begin muxes it back to the controller, and every caller that reads true goes // on to begin. -bool SDSlotFilled() +FLASHMEM bool SDSlotFilled() { // _SD_DAT3 = pin 46 pinMode(46, INPUT_PULLDOWN); From c898bcf6d0a21115dd49ac5976ab93538effe914 Mon Sep 17 00:00:00 2001 From: Kelly Fox Date: Mon, 26 Oct 2026 10:04:44 -0500 Subject: [PATCH 5/5] firmware: the Installed Extension page begins the card through SDBegin Each draw that finds no begun card restarts it, and the menu's last read can still be open then, so it ends that read first as SDFullInit and MenuChange do. --- Source/Teensy/DriveDirLoad.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Teensy/DriveDirLoad.ino b/Source/Teensy/DriveDirLoad.ino index 68662bff3..c611af9bc 100644 --- a/Source/Teensy/DriveDirLoad.ino +++ b/Source/Teensy/DriveDirLoad.ino @@ -119,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