utils: mkdos33fs: improve some more

This commit is contained in:
Vince Weaver 2021-03-29 14:25:58 -04:00
parent a2a9d6ef52
commit 8e05dc8b32
2 changed files with 99 additions and 52 deletions

View File

@ -2,6 +2,9 @@
#include "dos33.h"
/* FIXME: this code assumes that we have 16 sectors pretty much everywhere */
static int ones_lookup[16]={
/* 0x0 = 0000 */ 0,
/* 0x1 = 0001 */ 1,

View File

@ -32,12 +32,14 @@ static void usage(char *binary,int help) {
int main(int argc, char **argv) {
int num_tracks=35,num_sectors=16,block_size=256;
int num_tracks=35,num_sectors=16,sector_size=256;
int max_files=105,catalog_track=17,vtoc_track=17,volume_number=254;
int catalog_sectors,current_sector;
int max_ts_pairs=255;
int fd,dos_fd;
char device[BUFSIZ],dos_src[BUFSIZ];
char *vtoc_buffer=NULL,*sector_buffer=NULL,*endptr;
unsigned char *vtoc_buffer=NULL,*sector_buffer=NULL;
char *endptr;
int i,c,copy_dos=0;
int result;
@ -59,7 +61,7 @@ int main(int argc, char **argv) {
break;
case 'b':
block_size=strtol(optarg,&endptr,10);
sector_size=strtol(optarg,&endptr,10);
if ( endptr == optarg ) usage(argv[0], 1);
break;
@ -98,35 +100,40 @@ int main(int argc, char **argv) {
strncpy(device,argv[optind],BUFSIZ-1);
/***********************/
/* Sanity check values */
/***********************/
/* s 2->32 (limited by 4-byte bitfields) */
/* sectors: 2->32 (limited by 4-byte bitfields) */
if ((num_sectors<2) || (num_sectors>32)) {
printf("Number of sectors must be >2 and <=32\n\n");
goto end_of_program;
}
/* t 1 ->(block_size-0x38)/2 */
if ((num_tracks<1) || (num_tracks>(block_size-0x38)/2)) {
printf("Number of tracks must be >18 and <=%i (block_size-0x38)/2\n\n",
(block_size-0x38)/2);
/* tracks 18-50 ->(sector_size-0x38)/4 */
/* limited by VTOC room for freespace bitmap (which is $38 to $ff) */
/* VOTC is always on track 17 so we need at least that many */
/* though could double if we used the unused sector fields */
if ((num_tracks<vtoc_track) || (num_tracks>(sector_size-0x38)/4)) {
printf("Number of tracks must be >%d and <=%i\n\n",
vtoc_track,(sector_size-0x38)/4);
goto end_of_program;
}
/* sector_size 256->65536 (or 512 based on one byte t/s size field?) */
if ((block_size<256)||(block_size>65536)) {
if ((sector_size<256)||(sector_size>65536)) {
printf("Block size must be >=256 and <65536\n\n");
goto end_of_program;
}
/* allocate space for buffers */
vtoc_buffer=calloc(1,sizeof(char)*block_size);
vtoc_buffer=calloc(1,sizeof(char)*sector_size);
if (vtoc_buffer==NULL) {
fprintf(stderr,"Error allocating memory!\n");
goto end_of_program;
}
sector_buffer=calloc(1,sizeof(char)*block_size);
sector_buffer=calloc(1,sizeof(char)*sector_size);
if (sector_buffer==NULL) {
fprintf(stderr,"Error allocating memory!\n");
goto end_of_program;
@ -143,7 +150,7 @@ int main(int argc, char **argv) {
/* zero out file */
for(i=0;i<num_tracks*num_sectors;i++) {
result=write(fd,vtoc_buffer,block_size);
result=write(fd,vtoc_buffer,sector_size);
}
/**************************/
@ -171,12 +178,19 @@ int main(int argc, char **argv) {
vtoc_track);
}
max_ts_pairs=((block_size-0xc)/2);
max_ts_pairs=((sector_size-0xc)/2);
if (max_ts_pairs>255) {
printf("Warning! Truncating max_ts pairs to 255\n");
max_ts_pairs=255;
}
catalog_sectors=max_files/7;
if (max_files%7) catalog_sectors++;
if (catalog_sectors>num_sectors-1) {
printf("Warning! num_files leads to too many sectors %d, max is %d\n",catalog_sectors,num_sectors-1);
catalog_sectors=num_sectors-1;
}
/***************/
/* Create VTOC */
/***************/
@ -193,7 +207,7 @@ int main(int argc, char **argv) {
/* Number of T/S pairs fitting */
/* in a T/S list sector */
/* Note, overflows if block_size>524 */
/* Note, overflows if sector_size>524 */
vtoc_buffer[VTOC_MAX_TS_PAIRS]=max_ts_pairs;
/* last track space was allocated on */
@ -204,11 +218,11 @@ int main(int argc, char **argv) {
vtoc_buffer[VTOC_NUM_TRACKS]=num_tracks;
vtoc_buffer[VTOC_S_PER_TRACK]=num_sectors;
vtoc_buffer[VTOC_BYTES_PER_SL]=block_size&0xff;
vtoc_buffer[VTOC_BYTES_PER_SH]=(block_size>>8)&0xff;
vtoc_buffer[VTOC_BYTES_PER_SL]=sector_size&0xff;
vtoc_buffer[VTOC_BYTES_PER_SH]=(sector_size>>8)&0xff;
/* Set sector bitmap so whole disk is free */
for(i=VTOC_FREE_BITMAPS;i<block_size;i+=4) {
for(i=VTOC_FREE_BITMAPS;i<sector_size;i+=4) {
vtoc_buffer[i]=0xff;
vtoc_buffer[i+1]=0xff;
if (num_sectors>16) {
@ -228,36 +242,26 @@ int main(int argc, char **argv) {
lseek(fd,0,SEEK_SET);
/* copy first 3 sectors */
for(i=0;i<3*(num_sectors);i++) {
result=read(dos_fd,vtoc_buffer,block_size);
result=write(fd,vtoc_buffer,block_size);
result=read(dos_fd,vtoc_buffer,sector_size);
result=write(fd,vtoc_buffer,sector_size);
}
close(dos_fd);
/* Set boot filename */
/* Track 1 sector 9 */
lseek(fd,((1*num_sectors)+9)*block_size,SEEK_SET);
result=read(fd,vtoc_buffer,block_size);
lseek(fd,((1*num_sectors)+9)*sector_size,SEEK_SET);
result=read(fd,vtoc_buffer,sector_size);
/* filename begins at offset 75 */
for(i=0;i<30;i++) {
vtoc_buffer[0x75+i]=boot_filename[i]|0x80;
}
lseek(fd,((1*num_sectors)+9)*block_size,SEEK_SET);
result=write(fd,vtoc_buffer,block_size);
}
lseek(fd,((1*num_sectors)+9)*sector_size,SEEK_SET);
result=write(fd,vtoc_buffer,sector_size);
/* if copying dos reserve tracks 1 and 2 as well */
/* reserve track 0 */
/* No user data can be stored here as track=0 is special case */
/* end of file indicator */
vtoc_buffer[VTOC_FREE_BITMAPS]=0x00;
vtoc_buffer[VTOC_FREE_BITMAPS+1]=0x00;
vtoc_buffer[VTOC_FREE_BITMAPS+2]=0x00;
vtoc_buffer[VTOC_FREE_BITMAPS+3]=0x00;
/* if copying dos reserve tracks 1 and 2 as well */
if (copy_dos) {
vtoc_buffer[VTOC_FREE_BITMAPS+4]=0x00;
vtoc_buffer[VTOC_FREE_BITMAPS+5]=0x00;
vtoc_buffer[VTOC_FREE_BITMAPS+6]=0x00;
@ -266,36 +270,76 @@ int main(int argc, char **argv) {
vtoc_buffer[VTOC_FREE_BITMAPS+9]=0x00;
vtoc_buffer[VTOC_FREE_BITMAPS+10]=0x00;
vtoc_buffer[VTOC_FREE_BITMAPS+11]=0x00;
}
/* reserve track 17 (0x11) */
/* reserved for vtoc and catalog stuff */
vtoc_buffer[VTOC_FREE_BITMAPS+17*4]=0x00;
vtoc_buffer[VTOC_FREE_BITMAPS+17*4+1]=0x00;
vtoc_buffer[VTOC_FREE_BITMAPS+17*4+2]=0x00;
vtoc_buffer[VTOC_FREE_BITMAPS+17*4+3]=0x00;
/* reserve track 0 */
/* No user data can be stored here as track=0 is used */
/* as a special-case end of file indicator */
for(i=0;i<num_sectors;i++) {
dos33_vtoc_reserve_sector(vtoc_buffer, 0, i);
}
/********************/
/* reserve the VTOC */
/********************/
dos33_vtoc_reserve_sector(vtoc_buffer, vtoc_track, 0);
/*****************/
/* Setup Catalog */
/*****************/
/* clear buffer */
memset(sector_buffer,0,sector_size);
/* Set catalog next pointers */
for(i=0;i<catalog_sectors;i++) {
/* point to next */
/* for first sector_size-1 walk backwards from T17S15 */
/* if more, allocate room on disk??? */
/* Max on 140k disk is 280 or so */
current_sector=num_sectors-i-1;
if (i==catalog_sectors-1) {
/* last one, pointer is to 0,0 */
sector_buffer[1]=0;
sector_buffer[2]=0;
}
else {
printf("Writing $%02X,$%02X=%d,%d\n",
catalog_track,current_sector,
catalog_track,current_sector-1);
sector_buffer[1]=catalog_track;
sector_buffer[2]=current_sector-1;
}
/* reserve */
dos33_vtoc_reserve_sector(vtoc_buffer,
catalog_track, current_sector);
lseek(fd,((catalog_track*num_sectors)+current_sector)*
sector_size,SEEK_SET);
result=write(fd,sector_buffer,sector_size);
if (result!=sector_size) {
fprintf(stderr,"Error writing catalog sector %d! (%s)\n",
current_sector,strerror(errno));
}
}
/**************************/
/* Write out VTOC to disk */
/**************************/
lseek(fd,((vtoc_track*num_sectors)+0)*block_size,SEEK_SET);
lseek(fd,((vtoc_track*num_sectors)+0)*sector_size,SEEK_SET);
result=write(fd,vtoc_buffer,block_size);
result=write(fd,vtoc_buffer,sector_size);
if (result<0) {
fprintf(stderr,"Error writing VTOC (%s)!\n",strerror(errno));
}
/* clear vtoc_buffer */
for(i=0;i<block_size;i++) vtoc_buffer[i]=0;
/* Set catalog next pointers */
for(i=(num_sectors-1);i>1;i--) {
vtoc_buffer[1]=0x11;
vtoc_buffer[2]=i-1;
lseek(fd,((17*num_sectors)+i)*block_size,SEEK_SET);
result=write(fd,vtoc_buffer,block_size);
if (result<0) fprintf(stderr,"Error writing!\n");
}
close(fd);