mirror of
https://github.com/marqs85/ossc.git
synced 2025-02-05 12:33:24 +00:00
Userdata export: export settings on a FAT16 filesystem.
This commit is contained in:
parent
015f63ddff
commit
8068542da1
@ -159,6 +159,7 @@ C_SRCS += ossc/av_controller.c
|
||||
C_SRCS += ossc/avconfig.c
|
||||
C_SRCS += ossc/controls.c
|
||||
C_SRCS += ossc/firmware.c
|
||||
C_SRCS += ossc/fat16_export.c
|
||||
ifeq ($(OSDLANG),JP)
|
||||
C_SRCS += ossc/menu_sjis.c
|
||||
else
|
||||
|
File diff suppressed because it is too large
Load Diff
138
software/sys_controller/ossc/fat16_export.c
Normal file
138
software/sys_controller/ossc/fat16_export.c
Normal file
@ -0,0 +1,138 @@
|
||||
//
|
||||
// Copyright (C) 2020 Ari Sundholm <megari@iki.fi>
|
||||
//
|
||||
// This file has been contributed to the Open Source Scan Converter project
|
||||
// developed by Markus Hiienkari Markus Hiienkari <mhiienka@niksula.hut.fi>
|
||||
// and other members of the retro gaming community.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
#include <string.h>
|
||||
#include "fat16_export.h"
|
||||
|
||||
/*
|
||||
* The beginning of the boot sector. Will be followed by the BPB.
|
||||
* Offsets 0x000 to 0x00a, inclusive.
|
||||
*/
|
||||
static const alt_u8 bootsec_before_bpb_16[11] = {
|
||||
0xeb, 0x00, 0x90, 0x4d, 0x53, 0x57, 0x49, 0x4e, 0x34, 0x2e, 0x31,
|
||||
/* ^-----------^--- Maybe zero these? */
|
||||
};
|
||||
|
||||
/* The BPB will span offsets 0x00b to 0x23, inclusive. Will be generated. */
|
||||
|
||||
/*
|
||||
* The rest of the boot sector before the boot code and terminator.
|
||||
* Offsets 0x024 to 0x03d, inclusive.
|
||||
*/
|
||||
static const alt_u8 bootsec_after_bpb_16[26] = {
|
||||
0x80, 0x00, 0x29, 0xf4, 0xcf, 0xc6, 0x04, 0x4f, 0x53, 0x53, 0x43, 0x50,
|
||||
0x52, 0x4f, 0x46, 0x49, 0x4c, 0x53, 0x46, 0x41, 0x54, 0x31, 0x36, 0x20,
|
||||
0x20, 0x20,
|
||||
};
|
||||
|
||||
/*
|
||||
* After this, we have the boot code (448 bytes) and sector terminator
|
||||
* (2 bytes). These will be generated.
|
||||
*/
|
||||
|
||||
/* Generates a FAT16 boot sector.
|
||||
* buf must be at least FAT16_SECTOR_SIZE bytes long,
|
||||
* and is assumed to be pre-zeroed.
|
||||
*/
|
||||
void generate_boot_sector_16(alt_u8 *const buf) {
|
||||
/* Initial FAT16 boot sector contents. */
|
||||
memcpy(buf, bootsec_before_bpb_16, 11);
|
||||
|
||||
/* BPB */
|
||||
buf[12] = 0x02;
|
||||
buf[13] = 0x04;
|
||||
buf[14] = 0x80;
|
||||
buf[16] = 0x02;
|
||||
buf[18] = 0x08;
|
||||
buf[20] = 0x80;
|
||||
buf[21] = 0xf8;
|
||||
buf[22] = 0x20;
|
||||
buf[24] = 0x3f;
|
||||
buf[26] = 0xff;
|
||||
|
||||
/*
|
||||
* Then the rest of the boot sector.
|
||||
* The boot code is just 448 bytes of 0xf4.
|
||||
*/
|
||||
memcpy(buf + 36, bootsec_after_bpb_16, 26);
|
||||
memset(buf + 62, 0xf4, 448);
|
||||
buf[510] = 0x55;
|
||||
buf[511] = 0xaa;
|
||||
}
|
||||
|
||||
/* The fixed 'preamble' of a FAT on a FAT16 volume. */
|
||||
static const alt_u8 fat16_preamble[4] = {
|
||||
0xf8, 0xff, 0xff, 0xff,
|
||||
};
|
||||
|
||||
#if 0
|
||||
/*
|
||||
* To generate each FAT, we need 1040 bytes of memory.
|
||||
* The buffer is assumed to be zeroed out.
|
||||
* XXX: This is a problem!
|
||||
*/
|
||||
#define FAT16_ALLOC_SIZE 0x420U
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Generate a FAT.
|
||||
* The buffer is assumed to be zeroed out and have a size of at least
|
||||
* FAT16_SECTOR_SIZE bytes.
|
||||
* The number of clusters already written is given as an argument.
|
||||
* The function returns the total number of clusters written so far.
|
||||
*
|
||||
* The intention is to be able to generate and write the FAT in chunks
|
||||
* that do not exhaust all the remaining RAM.
|
||||
*/
|
||||
alt_u16 generate_fat16(alt_u8 *const fat, const alt_u16 written) {
|
||||
alt_u16 cur_ofs = 0;
|
||||
const alt_u16 start_cluster = 3U + written;
|
||||
const alt_u16 clusters_to_write = !written
|
||||
? ((FAT16_SECTOR_SIZE - sizeof(fat16_preamble)) >> FAT16_ENTRY_SHIFT)
|
||||
: (((514U - written) > (512U >> FAT16_ENTRY_SHIFT))
|
||||
? (512U >> FAT16_ENTRY_SHIFT)
|
||||
: (514U - written));
|
||||
const alt_u16 end_cluster = start_cluster + clusters_to_write;
|
||||
|
||||
if (!written) {
|
||||
memcpy(fat, fat16_preamble, sizeof(fat16_preamble));
|
||||
cur_ofs += sizeof(fat16_preamble);
|
||||
}
|
||||
|
||||
for (alt_u16 cluster = start_cluster; cluster < end_cluster; ++cluster) {
|
||||
/* FAT16 entries are 16-bit little-endian. */
|
||||
fat[cur_ofs++] = cluster & 0xffU;
|
||||
fat[cur_ofs++] = (cluster >> 8U) & 0xffU;
|
||||
}
|
||||
|
||||
/* After the last cluster, write the chain terminator. */
|
||||
if (end_cluster == 514U) {
|
||||
fat[cur_ofs++] = 0xff;
|
||||
fat[cur_ofs++] = 0xff;
|
||||
}
|
||||
return end_cluster;
|
||||
}
|
||||
|
||||
const alt_u8 prof_dirent_16[PROF_DIRENT_16_SIZE] = {
|
||||
0x4f, 0x53, 0x53, 0x43, 0x50, 0x52, 0x4f, 0x46, 0x42, 0x49, 0x4e, 0x20,
|
||||
0x00, 0x8e, 0x04, 0xb5, 0x6f, 0x51, 0x6f, 0x51, 0x00, 0x00, 0x17, 0x89,
|
||||
0x6f, 0x51, 0x02, 0x00, 0x00, 0x00, 0x10, 0x00,
|
||||
};
|
76
software/sys_controller/ossc/fat16_export.h
Normal file
76
software/sys_controller/ossc/fat16_export.h
Normal file
@ -0,0 +1,76 @@
|
||||
//
|
||||
// Copyright (C) 2020 Ari Sundholm <megari@iki.fi>
|
||||
//
|
||||
// This file has been contributed to the Open Source Scan Converter project
|
||||
// developed by Markus Hiienkari Markus Hiienkari <mhiienka@niksula.hut.fi>
|
||||
// and other members of the retro gaming community.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
#ifndef FAT16_EXPORT_H_
|
||||
#define FAT16_EXPORT_H_
|
||||
|
||||
#include "alt_types.h"
|
||||
|
||||
/* Use a sector size of 512 bytes. */
|
||||
#define FAT16_SECTOR_SIZE 512U
|
||||
//#define FAT16_BOOT_SECTOR_SIZE FAT16_SECTOR_SIZE
|
||||
|
||||
/* This volume has 2048-byte clusters. */
|
||||
#define FAT16_CLUSTER_SIZE 2048U
|
||||
|
||||
/* Offsets of the two File Allocation Tables. */
|
||||
#define FAT16_1_OFS 0x10000UL
|
||||
#define FAT16_2_OFS 0x14000UL
|
||||
|
||||
|
||||
/* Each FAT16 entry is a 16-bit little-endian integer. */
|
||||
#define FAT16_ENTRY_SIZE 2U
|
||||
#define FAT16_ENTRY_SHIFT 1
|
||||
|
||||
/* On this volume, each FAT will be 16 kiB in size. */
|
||||
#define FAT16_SIZE 0x4000U
|
||||
|
||||
/*
|
||||
* Define the properties and contents of the directory entry for the
|
||||
* settings file.
|
||||
*/
|
||||
#define PROF_DIRENT_16_OFS 0x18000UL
|
||||
#define PROF_DIRENT_16_SIZE 32UL
|
||||
|
||||
extern const alt_u8 prof_dirent_16[PROF_DIRENT_16_SIZE];
|
||||
|
||||
#define PROF_16_DATA_OFS 0x028000UL
|
||||
#define PROF_16_DATA_SIZE 0x100000UL
|
||||
/* Profile file data starts at offset 0x00028000 */
|
||||
/* Profile file data ends at offset 0x00128000 */
|
||||
/* Profile file data is exactly 1 MiB long. */
|
||||
|
||||
|
||||
/* Generate a FAT16 boot sector.
|
||||
* buf must be at least FAT16_BOOT_SECTOR_SIZE bytes long,
|
||||
* and is assumed to be pre-zeroed.
|
||||
*/
|
||||
void generate_boot_sector_16(alt_u8 *buf);
|
||||
|
||||
/*
|
||||
* Generate a FAT of a FAT16 volume.
|
||||
* The buffer is assumed to be zeroed out and have a size of at least 512 bytes.
|
||||
* The number of clusters already written are given as an argument.
|
||||
* The function returns the total number of clusters written so far.
|
||||
*/
|
||||
alt_u16 generate_fat16(alt_u8 *fat, alt_u16 written);
|
||||
|
||||
#endif // FAT16_EXPORT_H_
|
@ -21,6 +21,7 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "userdata.h"
|
||||
#include "fat16_export.h"
|
||||
#include "flash.h"
|
||||
#include "sdcard.h"
|
||||
#include "firmware.h"
|
||||
@ -413,8 +414,45 @@ eval_button:
|
||||
strncpy(menu_row2, "Exporting...", LCD_ROW_LEN+1);
|
||||
ui_disp_menu(2);
|
||||
|
||||
/* TODO: write FAT */
|
||||
_Static_assert(SD_BLK_SIZE == FAT16_SECTOR_SIZE, "Sector size mismatch");
|
||||
|
||||
/* Generate and write the boot sector. */
|
||||
memset(databuf, 0, SD_BLK_SIZE); /* Must be at least 512 bytes! */
|
||||
generate_boot_sector_16(databuf);
|
||||
SD_Write(&sdcard_dev, databuf, 0);
|
||||
/* ERROR HANDLING */
|
||||
|
||||
/* Generate and write the file allocation tables. */
|
||||
for (alt_u16 clusters_written = 0, sd_blk_idx = 0;
|
||||
clusters_written < (PROF_16_DATA_SIZE/FAT16_CLUSTER_SIZE);)
|
||||
{
|
||||
alt_u16 count;
|
||||
memset(databuf, 0, SD_BLK_SIZE); /* Must be at least 512 bytes! */
|
||||
count = generate_fat16(databuf, clusters_written);
|
||||
SD_Write(&sdcard_dev, databuf,
|
||||
(FAT16_1_OFS/SD_BLK_SIZE) + sd_blk_idx);
|
||||
/* ERROR HANDLING */
|
||||
|
||||
SD_Write(&sdcard_dev, databuf,
|
||||
(FAT16_2_OFS/SD_BLK_SIZE) + sd_blk_idx);
|
||||
/* ERROR HANDLING */
|
||||
|
||||
++sd_blk_idx;
|
||||
clusters_written += count;
|
||||
}
|
||||
|
||||
/* Write the directory entry of the settings file. */
|
||||
memset(databuf, 0, SD_BLK_SIZE);
|
||||
memcpy(databuf, prof_dirent_16, PROF_DIRENT_16_SIZE);
|
||||
SD_Write(&sdcard_dev, databuf, PROF_DIRENT_16_OFS/SD_BLK_SIZE);
|
||||
/* ERROR HANDLING */
|
||||
|
||||
/* This may wear the SD card a bit more than necessary... */
|
||||
retval = copy_flash_to_sd(USERDATA_OFFSET/PAGESIZE, 512/SD_BLK_SIZE, (MAX_USERDATA_ENTRY + 1) * SECTORSIZE, databuf);
|
||||
retval = copy_flash_to_sd(USERDATA_OFFSET/PAGESIZE,
|
||||
PROF_16_DATA_OFS/SD_BLK_SIZE,
|
||||
(MAX_USERDATA_ENTRY + 1) * SECTORSIZE,
|
||||
databuf);
|
||||
|
||||
out:
|
||||
SPI_CS_High();
|
||||
|
Loading…
x
Reference in New Issue
Block a user