|
|
1.1 root 1: /* 1.1.1.4 root 2: Hatari - createBlankImage.c 1.1.1.5 ! root 3: 1.1.1.4 root 4: This file is distributed under the GNU Public License, version 2 or at 5: your option any later version. Read the file gpl.txt for details. 1.1.1.5 ! root 6: 1.1 root 7: Create Blank .ST/.MSA Disc Images 8: */ 1.1.1.5 ! root 9: char CreateBlankImage_rcsid[] = "Hatari $Id: createBlankImage.c,v 1.10 2004/04/30 19:23:45 thothy Exp $"; 1.1 root 10: 11: #include "main.h" 1.1.1.4 root 12: #include "configuration.h" 1.1.1.5 ! root 13: #include "dim.h" 1.1 root 14: #include "file.h" 15: #include "floppy.h" 16: #include "misc.h" 17: #include "msa.h" 18: #include "st.h" 1.1.1.5 ! root 19: #include "createBlankImage.h" 1.1.1.3 root 20: 1.1 root 21: /*-----------------------------------------------------------------------*/ 22: /* 23: 40 track SS 40 track DS 80 track SS 80 track DS 24: 0- 1 Branch instruction to boot program if executable 25: 2- 7 'Loader' 26: 8-10 24-bit serial number 27: 11-12 BPS 512 512 512 512 28: 13 SPC 1 2 2 2 29: 14-15 RES 1 1 1 1 30: 16 FAT 2 2 2 2 31: 17-18 DIR 64 112 112 112 32: 19-20 SEC 360 720 720 1440 1.1.1.5 ! root 33: 21 MEDIA $FC $FD $F8 $F9 (isn't used by ST-BIOS) 1.1 root 34: 22-23 SPF 2 2 5 5 35: 24-25 SPT 9 9 9 9 36: 26-27 SIDE 1 2 1 2 37: 28-29 HID 0 0 0 0 38: 510-511 CHECKSUM 39: */ 40: 1.1.1.2 root 41: 42: /*-----------------------------------------------------------------------*/ 1.1 root 43: /* 1.1.1.5 ! root 44: Calculate the size of a disc in dialog 1.1 root 45: */ 1.1.1.5 ! root 46: static int CreateBlankImage_GetDiscImageCapacity(int nTracks, int nSectors, int nSides) 1.1 root 47: { 1.1.1.5 ! root 48: /* Find size of disc image */ ! 49: return nTracks*nSectors*nSides*NUMBYTESPERSECTOR; 1.1 root 50: } 51: 1.1.1.2 root 52: 53: /*-----------------------------------------------------------------------*/ 1.1 root 54: /* 1.1.1.5 ! root 55: Write a short integer to addr using little endian byte order ! 56: (needed for 16 bit values in the bootsector of the disc image). 1.1 root 57: */ 1.1.1.5 ! root 58: static inline void WriteShortLE(void *addr, Uint16 val) 1.1 root 59: { 1.1.1.5 ! root 60: Uint8 *p = (Uint8 *)addr; 1.1 root 61: 1.1.1.5 ! root 62: p[0] = (Uint8)val; ! 63: p[1] = (Uint8)(val >> 8); 1.1 root 64: } 65: 1.1.1.2 root 66: 67: /*-----------------------------------------------------------------------*/ 1.1 root 68: /* 1.1.1.5 ! root 69: Create .ST/.MSA disc image according to 'Tracks,Sector,Sides' and save as filename 1.1 root 70: */ 1.1.1.5 ! root 71: void CreateBlankImage_CreateFile(char *pszFileName, int nTracks, int nSectors, int nSides) 1.1 root 72: { 1.1.1.5 ! root 73: Uint8 *pDiscFile; ! 74: unsigned long DiscSize; ! 75: unsigned short int SPC, DIR, MediaByte, SPF; ! 76: BOOL bRet=FALSE; ! 77: ! 78: /* Calculate size of disc image */ ! 79: DiscSize = CreateBlankImage_GetDiscImageCapacity(nTracks, nSectors, nSides); ! 80: ! 81: /* Allocate space for our 'file', and blank */ ! 82: pDiscFile = malloc(DiscSize); ! 83: if (pDiscFile == NULL) ! 84: { ! 85: perror("Error while creating blank disc image"); ! 86: return; ! 87: } ! 88: memset(pDiscFile, 0, DiscSize); /* Clear buffer */ ! 89: ! 90: /* Fill in boot-sector */ ! 91: pDiscFile[0] = 0xE9; /* Needed for MS-DOS compatibility */ ! 92: memset(pDiscFile+2, 0x4e, 6); /* 2-7 'Loader' */ ! 93: ! 94: WriteShortLE(pDiscFile+8, rand()); /* 8-10 24-bit serial number */ ! 95: pDiscFile[10] = rand(); ! 96: ! 97: WriteShortLE(pDiscFile+11, NUMBYTESPERSECTOR); /* 11-12 BPS */ ! 98: ! 99: if ((nTracks == 40) && (nSides == 1)) ! 100: SPC = 1; ! 101: else ! 102: SPC = 2; ! 103: pDiscFile[13] = SPC; /* 13 SPC */ ! 104: ! 105: WriteShortLE(pDiscFile+14, 1); /* 14-15 RES */ ! 106: pDiscFile[16] = 2; /* 16 FAT */ ! 107: ! 108: if (SPC==1) ! 109: DIR = 64; ! 110: else ! 111: DIR = 112; ! 112: WriteShortLE(pDiscFile+17, DIR); /* 17-18 DIR */ ! 113: ! 114: WriteShortLE(pDiscFile+19, nTracks*nSectors*nSides); /* 19-20 SEC */ ! 115: ! 116: if (nTracks <= 42) ! 117: MediaByte = 0xFC; ! 118: else ! 119: MediaByte = 0xF8; ! 120: if (nSides == 2) ! 121: MediaByte |= 0x01; ! 122: pDiscFile[21] = MediaByte; /* 21 MEDIA */ ! 123: ! 124: if (nTracks >= 80) ! 125: SPF = 5; ! 126: else ! 127: SPF = 2; ! 128: WriteShortLE(pDiscFile+22, SPF); /* 22-23 SPF */ ! 129: ! 130: WriteShortLE(pDiscFile+24, nSectors); /* 24-25 SPT */ ! 131: WriteShortLE(pDiscFile+26, nSides); /* 26-27 SIDE */ ! 132: WriteShortLE(pDiscFile+28, 0); /* 28-29 HID */ ! 133: ! 134: /* Set correct media bytes in the 1st FAT: */ ! 135: pDiscFile[512] = MediaByte; ! 136: pDiscFile[513] = pDiscFile[514] = 0xFF; ! 137: /* Set correct media bytes in the 2nd FAT: */ ! 138: pDiscFile[512 + SPF * 512] = MediaByte; ! 139: pDiscFile[513 + SPF * 512] = pDiscFile[514 + SPF * 512] = 0xFF; ! 140: ! 141: /* Ask if OK to overwrite, if exists? */ ! 142: if (File_QueryOverwrite(pszFileName)) ! 143: { ! 144: /* Save image to file */ ! 145: if (MSA_FileNameIsMSA(pszFileName, TRUE)) ! 146: bRet = MSA_WriteDisc(pszFileName, pDiscFile, DiscSize); ! 147: else if (ST_FileNameIsST(pszFileName, TRUE)) ! 148: bRet = ST_WriteDisc(pszFileName, pDiscFile, DiscSize); ! 149: else if (DIM_FileNameIsDIM(pszFileName, TRUE)) ! 150: bRet = DIM_WriteDisc(pszFileName, pDiscFile, DiscSize); ! 151: ! 152: /* Did create successfully? */ ! 153: if (bRet) ! 154: { ! 155: /* Say OK, */ ! 156: Main_Message("Disc image created successfully.", PROG_NAME /*,MB_OK | MB_ICONINFORMATION*/); ! 157: } ! 158: else ! 159: { ! 160: /* Warn user we were unable to create image */ ! 161: Main_Message("Unable to create disc image!", PROG_NAME /*,MB_OK | MB_ICONSTOP*/); ! 162: } ! 163: } 1.1 root 164: 1.1.1.5 ! root 165: /* Free image */ ! 166: free(pDiscFile); 1.1 root 167: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.