Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions Source/Teensy/DriveDirLoad.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
124 changes: 52 additions & 72 deletions Source/Teensy/MinimalBoot/Common/VMPackageList.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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 e.id;i++)e.id[i]=shown(name[i]);
if(!n||n>=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.
Expand All @@ -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;i<s.count;i++)
for(unsigned j=i;j&&strcasecmp(s.entry[j-1].id,s.entry[j].id)>0;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;}
Expand Down Expand Up @@ -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;i<s.count;i++)listed+=s.entry[i].state==Listed;

// At most PackageRows, the last of them "+N more", so a row is always left for why.
const unsigned packages=s.count>PackageRows?PackageRows-1:s.count;
for(unsigned i=0;i<packages;i++)packageRow(out,s,i);
if(packages<s.count)add(out," +%u more",s.count-packages);

// The whole of /VMS. Over the limit or on a read error, find() refuses every launch.
if(!s.cardRead)add(out," No SD card, or it can't be read.");
else if(s.walk==WalkNoDirectory)add(out," No /VMS folder on the SD card.");
else if(s.walk==WalkOverLimit)add(out," Over %u folders in /VMS: none launch.",(unsigned)FolderLimit);
else if(s.walk==WalkError)add(out," /VMS read error: none launch.");
else if(!s.count)add(out," No packages in /VMS.");
if(!s.cardRead)add(reasons," No SD card, or it can't be read.");
else if(s.walk==VmRegistry::WalkNoDirectory)add(reasons," No /VMS folder on the SD card.");
else if(s.walk==VmRegistry::WalkOverLimit)add(reasons," Over %u folders in /VMS: none launch.",(unsigned)VmRegistry::FolderLimit);
else if(s.walk==VmRegistry::WalkError)add(reasons," /VMS read error: none launch.");
else if(!s.count)add(reasons," No packages in /VMS.");

// The host refuses every package or none of them on these two.
if(listed){
if(!installed)add(out," No host installed, so none launch.");
else if(host&&host->abi!=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.
Expand All @@ -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;j<s.count;j++)if(j!=i&&claims(s.entry[j],ext)){if(j<i)earlier=true;else if(!others++)other=j;}
if(earlier||!others)continue;
if(others==1)add(out," *.%s claimed by %s and %s",ext,s.entry[i].id,s.entry[other].id);
else add(out," *.%s claimed by %u packages",ext,others+1);
if(others==1)add(reasons," *.%s claimed by %s and %s",ext,s.entry[i].id,s.entry[other].id);
else add(reasons," *.%s claimed by %u packages",ext,others+1);
}
}

Expand All @@ -194,12 +160,12 @@ static FLASHMEM unsigned layout(const Scan &s,bool installed,const VmHostId *hos
const Entry &e=s.entry[i];
if(e.state!=Listed)continue;
switch(e.refusal){
case ModuleFile:add(out," !%s: module file bad or missing",e.id);break;
case ClientFile:add(out," !%s: client file bad or missing",e.id);break;
case Services:add(out," !%s: host lacks service $%lx",e.id,(unsigned long)e.detail);break;
case ModuleFile:add(reasons," !%s: module file bad or missing",e.id);break;
case ClientFile:add(reasons," !%s: client file bad or missing",e.id);break;
case Services:add(reasons," !%s: host lacks service $%lx",e.id,(unsigned long)e.detail);break;
case CodeBase:
if(!host||!host->code_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;
Expand All @@ -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=needed<ReservedNotes?needed:ReservedNotes;
const unsigned room=Rows-reserved;
const unsigned packages=s.count+needed<=Rows||s.count<=room?s.count:room-1;
Out out{rows,0,0};
for(unsigned i=0;i<packages;i++)packageRow(out,s,i);
if(packages<s.count)add(out," +%u more",s.count-packages);

const unsigned space=Rows-out.count;
const unsigned reasonsShown=needed<=space?reasons.wanted:space-1-message.count;
for(unsigned i=0;i<reasonsShown;i++)strcpy(out.row[out.count++],reasonRows[i]);
if(reasonsShown<reasons.wanted)add(out," +%u more notes",reasons.wanted-reasonsShown);
if(message.count)strcpy(out.row[out.count++],lastRow[0]);
return out.count;
}

Expand Down
Loading
Loading