void SendFileEncrypted(RAFile *file,unsigned char *key,int keysize) { unsigned char c1, c2; unsigned char headersize; unsigned int keyidx=0; unsigned long j; unsigned long n; unsigned char *p; int badbyte=0; iprintf("["); headersize=file->size&255; // ROM should be a round number of kilobytes; overspill will likely be the Amiga Forever header. RARead(file,sector_buffer,headersize); // Read extra bytes n = (file->size + (511-headersize)) >> 9; // sector count (rounded up) while (n--) { RARead(file,sector_buffer,512); for (j = 0; j < 512; j++) { sector_buffer[j]^=key[keyidx++]; if(keyidx>=keysize) keyidx-=keysize; } do { // read FPGA status EnableFpga(); c1 = SPI(0); c2 = SPI(0); SPI(0); SPI(0); SPI(0); SPI(0); DisableFpga(); } while (!(c1 & CMD_RDTRK)); if ((n & 15) == 0) iprintf("*"); // send data sector to FPGA EnableFpga(); c1 = SPI(0); c2 = SPI(0); SPI(0); SPI(0); SPI(0); SPI(0); p = sector_buffer; for (j = 0; j < 512; j++) SPI(*p++); DisableFpga(); } iprintf("]\r"); }
// SendFileV2 (for minimig_v2) void SendFileV2(RAFile* file, unsigned char* key, int keysize, int address, int size) { int i,j; unsigned int keyidx=0; iprintf("File size: %dkB\r", size>>1); iprintf("["); if (keysize) { // read header RARead(file, sector_buffer, 0xb); } for (i=0; i<size; i++) { if (!(i&31)) iprintf("*"); RARead(file, sector_buffer, 512); if (keysize) { // decrypt ROM for (j=0; j<512; j++) { sector_buffer[j] ^= key[keyidx++]; if(keyidx >= keysize) keyidx -= keysize; } } EnableOsd(); unsigned int adr = address + i*512; SPI(OSD_CMD_WR); SPIN(); SPIN(); SPIN(); SPIN(); SPI(adr&0xff); adr = adr>>8; SPI(adr&0xff); adr = adr>>8; SPIN(); SPIN(); SPIN(); SPIN(); SPI(adr&0xff); adr = adr>>8; SPI(adr&0xff); adr = adr>>8; SPIN(); SPIN(); SPIN(); SPIN(); for (j=0; j<512; j=j+4) { SPI(sector_buffer[j+0]); SPI(sector_buffer[j+1]); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPI(sector_buffer[j+2]); SPI(sector_buffer[j+3]); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); } DisableOsd(); } iprintf("]\r"); }
void SendFile(RAFile *file) { DEBUG_FUNC_IN(DEBUG_F_FPGA | DEBUG_L0); unsigned char c1, c2; unsigned long j; unsigned long n; unsigned char *p; printf("["); n = (file->file.size + 511) >> 9; // sector count (rounded up) while (n--) { // read data sector from memory card RARead(file,sector_buffer,512); do { // read FPGA status EnableFpga(); c1 = SPI(0); c2 = SPI(0); SPI(0); SPI(0); SPI(0); SPI(0); DisableFpga(); } while (!(c1 & CMD_RDTRK)); if ((n & 15) == 0) printf("*"); // send data sector to FPGA EnableFpga(); c1 = SPI(0); c2 = SPI(0); SPI(0); SPI(0); SPI(0); SPI(0); p = sector_buffer; for (j = 0; j < 512; j++) SPI(*p++); DisableFpga(); } printf("]\r"); DEBUG_FUNC_OUT(DEBUG_F_FPGA | DEBUG_L0); }
//// ini_getch() //// char ini_getch() { static int ini_pt = 0; if ((ini_pt&0x3ff) == 0x200) { // reload buffer #ifdef INI_PARSER_TEST fread(sector_buffer, sizeof(char), INI_BUF_SIZE, ini_fp); #else RARead(&ini_file, sector_buffer, INI_BUF_SIZE); #endif } #ifdef INI_PARSER_TEST if (ini_pt >= ini_size) return 0; #else if (ini_pt >= ini_file.file.size) return 0; #endif else return sector_buffer[(ini_pt++)&0x1ff]; }
//// UploadKickstart() //// char UploadKickstart(char *name) { int keysize=0; char filename[12]; strncpy(filename, name, 8); // copy base name strcpy(&filename[8], "ROM"); // add extension BootPrint("Checking for Amiga Forever key file:"); if(FileOpen(&file,"ROM KEY")) { keysize=file.size; if(file.size<sizeof(romkey)) { int c=0; while(c<keysize) { FileRead(&file, &romkey[c]); c+=512; FileNextSector(&file); } BootPrint("Loaded Amiga Forever key file"); } else { BootPrint("Amiga Forever keyfile is too large!"); } } BootPrint("Loading file: "); BootPrint(filename); if(minimig_v1()) { if (RAOpen(&romfile, filename)) { if (romfile.size == 0x80000) { // 512KB Kickstart ROM BootPrint("Uploading 512 KB Kickstart..."); PrepareBootUpload(0xF8, 0x08); SendFile(&romfile); return(1); } else if ((romfile.size == 0x8000b) && keysize) { // 512KB Kickstart ROM BootPrint("Uploading 512 KB Kickstart (Probably Amiga Forever encrypted...)"); PrepareBootUpload(0xF8, 0x08); SendFileEncrypted(&romfile,romkey,keysize); return(1); } else if (romfile.size == 0x40000) { // 256KB Kickstart ROM BootPrint("Uploading 256 KB Kickstart..."); PrepareBootUpload(0xF8, 0x04); SendFile(&romfile); return(1); } else if ((romfile.size == 0x4000b) && keysize) { // 256KB Kickstart ROM BootPrint("Uploading 256 KB Kickstart (Probably Amiga Forever encrypted..."); PrepareBootUpload(0xF8, 0x04); SendFileEncrypted(&romfile,romkey,keysize); return(1); } else { BootPrint("Unsupported ROM file size!"); } } else { siprintf(s, "No \"%s\" file!", filename); BootPrint(s); } } else { if (RAOpen(&romfile, filename)) { int i,j; unsigned int adr, size, base=0x180000, offset=0xc00000, data; puts("Uploading 512KB Kickstart ..."); size = ((romfile.file.size)+511)>>9; iprintf("File size: %d.%01dkB\r", romfile.file.size>>10, romfile.file.size&0x3ff); iprintf("["); for (i=0; i<size; i++) { if (!(i&31)) iprintf("*"); RARead(&romfile,sector_buffer,512); EnableOsd(); adr = 0xf80000 + i*512; SPI(OSD_CMD_WR); SPIN(); SPIN(); SPIN(); SPIN(); SPI(adr&0xff); adr = adr>>8; SPI(adr&0xff); adr = adr>>8; SPIN(); SPIN(); SPIN(); SPIN(); SPI(adr&0xff); adr = adr>>8; SPI(adr&0xff); adr = adr>>8; SPIN(); SPIN(); SPIN(); SPIN(); for (j=0; j<512; j=j+4) { SPI(sector_buffer[j+0]); SPI(sector_buffer[j+1]); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPI(sector_buffer[j+2]); SPI(sector_buffer[j+3]); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); SPIN(); } DisableOsd(); } iprintf("]\r"); return(1); } }
//// ini_parse() //// void ini_parse(const ini_cfg_t* cfg) { char line[INI_LINE_SIZE] = {0}; int section = INI_SECTION_INVALID_ID; int line_status; // open ini file #ifdef INI_PARSER_TEST if ((ini_fp = fopen(cfg->filename, "rb")) == NULL) { #else if (!RAOpen(&ini_file, cfg->filename)) { #endif ini_parser_debugf("Can't open file %s !", cfg->filename); return; } #ifdef INI_PARSER_TEST // save size fseek(ini_fp, 0L, SEEK_END); ini_size = ftell(ini_fp); fseek(ini_fp, 0L, SEEK_SET); #endif #ifdef INI_PARSER_TEST ini_parser_debugf("Opened file %s with size %d bytes.", cfg->filename, ini_size); #else ini_parser_debugf("Opened file %s with size %d bytes.", cfg->filename, ini_file.file.size); #endif // preload buffer #ifdef INI_PARSER_TEST fread(sector_buffer, sizeof(char), INI_BUF_SIZE, ini_fp); #else RARead(&ini_file, sector_buffer, INI_BUF_SIZE); #endif // parse ini while (1) { // get line line_status = ini_getline(line); // if valid line if (line_status != 1) { if (line[0] == INI_SECTION_START) { // if first char in line is INI_SECTION_START, get section section = ini_get_section(cfg, line); } else { // otherwise this is a variable, get it ini_get_var(cfg, section, line); } } // if end of file, stop if (line_status == INI_EOT) break; } #ifdef INI_PARSER_TEST // close file fclose(ini_fp); #endif } //// ini_save() //// void ini_save(const ini_cfg_t* cfg) { int section, var, ini_pt; char line[INI_LINE_SIZE] = {0}; // open ini file #ifdef INI_PARSER_TEST if ((ini_fp = fopen(cfg->filename, "wb")) == NULL) { #else { //#error #endif ini_parser_debugf("Can't open file %s !", cfg->filename); return; } // loop over sections for (section=0; section<cfg->nsections; section++) { ini_parser_debugf("writing section %s ...", cfg->sections[section].name); siprintf(line, "[%s]\n", cfg->sections[section].name); ini_pt = ini_putline(line); // loop over vars for (var=0; var<cfg->nvars; var++) { if (cfg->vars[var].section_id == cfg->sections[section].id) { ini_parser_debugf("writing var %s", cfg->vars[var].name); switch (cfg->vars[var].type) { case UINT8: case UINT16: case UINT32: siprintf(line, "%s=%u\n", cfg->vars[var].name, *(uint32_t*)(cfg->vars[var].var)); break; case INT8: case INT16: case INT32: siprintf(line, "%s=%d\n", cfg->vars[var].name, *(int32_t*)(cfg->vars[var].var)); break; #ifdef INI_ENABLE_FLOAT case FLOAT: siprintf(line, "%s=%f\n", cfg->vars[var].name, *(float*)(cfg->vars[var].var)); break; #endif case STRING: siprintf(line, "%s=\"%s\"\n", cfg->vars[var].name, (char*)(cfg->vars[var].var)); break; } ini_pt = ini_putline(line); } } } // in case the buffer is not written yet, write it now if (ini_pt) { #ifdef INI_PARSER_TEST fwrite(sector_buffer, sizeof(char), ini_pt, ini_fp); #else //#error #endif } }