Annotation of hatari/src/createBlankImage.c, revision 1.1.1.11

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.1.7   root        7:   Create blank .ST/.MSA disk images.
1.1       root        8: */
1.1.1.11! root        9: const char CreateBlankImage_fileid[] = "Hatari createBlankImage.c : " __DATE__ " " __TIME__;
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"
1.1.1.6   root       16: #include "log.h"
1.1       root       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.1.9   root       43: /**
                     44:  * Calculate the size of a disk in dialog.
                     45:  */
1.1.1.7   root       46: static int CreateBlankImage_GetDiskImageCapacity(int nTracks, int nSectors, int nSides)
1.1       root       47: {
1.1.1.7   root       48:        /* Find size of disk image */
1.1.1.5   root       49:        return nTracks*nSectors*nSides*NUMBYTESPERSECTOR;
1.1       root       50: }
                     51: 
1.1.1.2   root       52: 
                     53: /*-----------------------------------------------------------------------*/
1.1.1.9   root       54: /**
                     55:  * Write a short integer to addr using little endian byte order
                     56:  * (needed for 16 bit values in the bootsector of the disk image).
                     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.1.9   root       68: /**
                     69:  * Create .ST/.MSA disk image according to 'Tracks,Sector,Sides' and save as filename
                     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.7   root       73:        Uint8 *pDiskFile;
                     74:        unsigned long nDiskSize;
1.1.1.6   root       75:        unsigned short int SPC, nDir, MediaByte, SPF;
1.1.1.10  root       76:        bool bRet = false;
1.1.1.5   root       77: 
1.1.1.7   root       78:        /* Calculate size of disk image */
                     79:        nDiskSize = CreateBlankImage_GetDiskImageCapacity(nTracks, nSectors, nSides);
1.1.1.5   root       80: 
                     81:        /* Allocate space for our 'file', and blank */
1.1.1.7   root       82:        pDiskFile = malloc(nDiskSize);
                     83:        if (pDiskFile == NULL)
1.1.1.5   root       84:        {
1.1.1.7   root       85:                perror("Error while creating blank disk image");
1.1.1.5   root       86:                return;
                     87:        }
1.1.1.7   root       88:        memset(pDiskFile, 0, nDiskSize);                      /* Clear buffer */
1.1.1.5   root       89: 
                     90:        /* Fill in boot-sector */
1.1.1.7   root       91:        pDiskFile[0] = 0xE9;                                  /* Needed for MS-DOS compatibility */
                     92:        memset(pDiskFile+2, 0x4e, 6);                         /* 2-7 'Loader' */
1.1.1.5   root       93: 
1.1.1.7   root       94:        WriteShortLE(pDiskFile+8, rand());                    /* 8-10 24-bit serial number */
                     95:        pDiskFile[10] = rand();
1.1.1.5   root       96: 
1.1.1.7   root       97:        WriteShortLE(pDiskFile+11, NUMBYTESPERSECTOR);        /* 11-12 BPS */
1.1.1.5   root       98: 
                     99:        if ((nTracks == 40) && (nSides == 1))
                    100:                SPC = 1;
                    101:        else
                    102:                SPC = 2;
1.1.1.7   root      103:        pDiskFile[13] = SPC;                                  /* 13 SPC */
1.1.1.5   root      104: 
1.1.1.7   root      105:        WriteShortLE(pDiskFile+14, 1);                        /* 14-15 RES */
                    106:        pDiskFile[16] = 2;                                    /* 16 FAT */
1.1.1.5   root      107: 
                    108:        if (SPC==1)
1.1.1.6   root      109:                nDir = 64;
1.1.1.5   root      110:        else
1.1.1.6   root      111:                nDir = 112;
1.1.1.7   root      112:        WriteShortLE(pDiskFile+17, nDir);                     /* 17-18 DIR */
1.1.1.5   root      113: 
1.1.1.7   root      114:        WriteShortLE(pDiskFile+19, nTracks*nSectors*nSides);  /* 19-20 SEC */
1.1.1.5   root      115: 
                    116:        if (nTracks <= 42)
                    117:                MediaByte = 0xFC;
                    118:        else
                    119:                MediaByte = 0xF8;
                    120:        if (nSides == 2)
                    121:                MediaByte |= 0x01;
1.1.1.7   root      122:        pDiskFile[21] = MediaByte;                            /* 21 MEDIA */
1.1.1.5   root      123: 
                    124:        if (nTracks >= 80)
                    125:                SPF = 5;
                    126:        else
                    127:                SPF = 2;
1.1.1.7   root      128:        WriteShortLE(pDiskFile+22, SPF);                      /* 22-23 SPF */
1.1.1.5   root      129: 
1.1.1.7   root      130:        WriteShortLE(pDiskFile+24, nSectors);                 /* 24-25 SPT */
                    131:        WriteShortLE(pDiskFile+26, nSides);                   /* 26-27 SIDE */
                    132:        WriteShortLE(pDiskFile+28, 0);                        /* 28-29 HID */
1.1.1.5   root      133: 
                    134:        /* Set correct media bytes in the 1st FAT: */
1.1.1.7   root      135:        pDiskFile[512] = MediaByte;
                    136:        pDiskFile[513] = pDiskFile[514] = 0xFF;
1.1.1.5   root      137:        /* Set correct media bytes in the 2nd FAT: */
1.1.1.7   root      138:        pDiskFile[512 + SPF * 512] = MediaByte;
                    139:        pDiskFile[513 + SPF * 512] = pDiskFile[514 + SPF * 512] = 0xFF;
1.1.1.5   root      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))
1.1.1.7   root      146:                        bRet = MSA_WriteDisk(pszFileName, pDiskFile, nDiskSize);
1.1.1.5   root      147:                else if (ST_FileNameIsST(pszFileName, TRUE))
1.1.1.7   root      148:                        bRet = ST_WriteDisk(pszFileName, pDiskFile, nDiskSize);
1.1.1.5   root      149:                else if (DIM_FileNameIsDIM(pszFileName, TRUE))
1.1.1.7   root      150:                        bRet = DIM_WriteDisk(pszFileName, pDiskFile, nDiskSize);
1.1.1.5   root      151: 
                    152:                /* Did create successfully? */
                    153:                if (bRet)
                    154:                {
                    155:                        /* Say OK, */
1.1.1.6   root      156:                        Log_AlertDlg(LOG_INFO, "Disk image has been created successfully.");
1.1.1.5   root      157:                }
                    158:                else
                    159:                {
                    160:                        /* Warn user we were unable to create image */
1.1.1.6   root      161:                        Log_AlertDlg(LOG_ERROR, "Unable to create disk image!");
1.1.1.5   root      162:                }
                    163:        }
1.1       root      164: 
1.1.1.5   root      165:        /* Free image */
1.1.1.7   root      166:        free(pDiskFile);
1.1       root      167: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.