diskio.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "integer.h"
  2. #include "diskio.h"
  3. static volatile
  4. DSTATUS Stat = STA_NOINIT; /* Disk status */
  5. <<<<<<< HEAD:tools/ffsample/linux/diskio.c
  6. /*
  7. sudo losetup /dev/loop0 disk00.vfat
  8. sudo mkfs.vfat -f 2 -F 16 -v /dev/loop0
  9. mkfs.vfat 2.11 (12 Mar 2005)
  10. Loop device does not match a floppy size, using default hd params
  11. /dev/loop0 has 64 heads and 32 sectors per track,
  12. logical sector size is 512,
  13. using 0xf8 media descriptor, with 524288 sectors;
  14. file system has 2 16-bit FATs and 8 sectors per cluster.
  15. FAT size is 256 sectors, and provides 65467 clusters.
  16. Root directory contains 512 slots.
  17. Volume ID is 4a1aab3d, no volume label.
  18. FAT type = 2
  19. Bytes/Cluster = 4096
  20. Number of FATs = 2
  21. Root DIR entries = 512
  22. Sectors/FAT = 256
  23. Number of clusters = 65467
  24. FAT start (lba) = 1
  25. DIR start (lba,clustor) = 513
  26. Data start (lba) = 545
  27. Ok
  28. disk_read: sector=513 count=1 addr=0xa8009800 size=512
  29. scan_files ret
  30. 0 files, 0 bytes.
  31. 0 folders.
  32. 261868 KB total disk space.
  33. 147456 KB available.
  34. Disk: /dev/disk1 geometry: 993/4/63 [250368 sectors]
  35. Signature: 0xAA55
  36. Starting Ending
  37. #: id cyl hd sec - cyl hd sec [ start - size]
  38. ------------------------------------------------------------------------
  39. 1: 0B 0 1 1 - 1023 254 63 [ 63 - 250299] Win95 FAT-32
  40. 2: 00 0 0 0 - 0 0 0 [ 0 - 0] unused
  41. 3: 00 0 0 0 - 0 0 0 [ 0 - 0] unused
  42. 4: 00 0 0 0 - 0 0 0 [ 0 - 0] unused
  43. */
  44. =======
  45. >>>>>>> 660767c464c3d78d36436123eb2e717c3c85f6bc:tools/ffsample/linux/diskio.c
  46. /* Interface
  47. ** Scratch Buffer
  48. addr 3 byte
  49. size 1 byte
  50. ** Call Interface
  51. cmd 1 byte
  52. sector 4 bytes
  53. count 1 byte
  54. return 1 byte
  55. ** Commands
  56. * disk_init
  57. * disk_read
  58. * disk_write
  59. */
  60. /*-----------------------------------------------------------------------*/
  61. /* Initialize Disk Drive */
  62. /*-----------------------------------------------------------------------*/
  63. #include <stdio.h>
  64. #include <stdlib.h>
  65. #include <sys/types.h>
  66. #include <sys/stat.h>
  67. #include <unistd.h>
  68. #include <fcntl.h>
  69. #include <sys/mman.h>
  70. #include <string.h>
  71. #define IMAGE_NAME "disk00.vfat"
  72. char *image_addr;
  73. DSTATUS disk_initialize (BYTE drv) {
  74. if (drv) return STA_NOINIT; /* Supports only single drive */
  75. Stat |= STA_NOINIT;
  76. int fd = open(IMAGE_NAME, O_RDWR);
  77. if (fd == -1) {
  78. perror("Error opening file for writing");
  79. exit(EXIT_FAILURE);
  80. }
  81. int size = lseek(fd,0,SEEK_END);
  82. lseek(fd,0,SEEK_SET);
  83. printf("Open Image (size %i)\n",size);
  84. image_addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  85. if (image_addr == MAP_FAILED) {
  86. close(fd);
  87. perror("Error mmapping the file");
  88. exit(EXIT_FAILURE);
  89. }
  90. Stat &= ~STA_NOINIT; /* When device goes ready, clear STA_NOINIT */
  91. return Stat;
  92. }
  93. /*-----------------------------------------------------------------------*/
  94. /* Return Disk Status */
  95. /*-----------------------------------------------------------------------*/
  96. DSTATUS disk_status (BYTE drv){
  97. if (drv) return STA_NOINIT; /* Supports only single drive */
  98. return Stat;
  99. }
  100. /*-----------------------------------------------------------------------*/
  101. /* Read Sector(s) */
  102. /*-----------------------------------------------------------------------*/
  103. DRESULT disk_read (
  104. BYTE drv, /* Physical drive nmuber (0) */
  105. BYTE *buff, /* Data buffer to store read data */
  106. DWORD sector, /* Sector number (LBA) */
  107. BYTE count /* Sector count (1..255) */
  108. )
  109. {
  110. BYTE c, iord_l, iord_h;
  111. if (drv || !count) return RES_PARERR;
  112. if (Stat & STA_NOINIT) return RES_NOTRDY;
  113. DWORD offset = sector * 512;
  114. int size = count * 512;
  115. printf("disk_read: sector=%li count=%i addr=%p off=%li size=%i\n",sector,count,image_addr + offset,offset,size);
  116. memcpy(buff,image_addr + offset,size);
  117. return RES_OK;
  118. }
  119. /*-----------------------------------------------------------------------*/
  120. /* Write Sector(s) */
  121. /*-----------------------------------------------------------------------*/
  122. #if _READONLY == 0
  123. DRESULT disk_write (
  124. BYTE drv, /* Physical drive nmuber (0) */
  125. const BYTE *buff, /* Data to be written */
  126. DWORD sector, /* Sector number (LBA) */
  127. BYTE count /* Sector count (1..255) */
  128. )
  129. {
  130. if (drv || !count) return RES_PARERR;
  131. if (Stat & STA_NOINIT) return RES_NOTRDY;
  132. DWORD offset = sector * 512;
  133. int size = count * 512;
  134. printf("disk_write: sector=%li count=%i addr=%p off=%li size=%i\n",sector,count,image_addr + offset,offset,size);
  135. memcpy(image_addr + offset,buff,size);
  136. return RES_OK;
  137. }
  138. #endif /* _READONLY */
  139. /*-----------------------------------------------------------------------*/
  140. /* Miscellaneous Functions */
  141. /*-----------------------------------------------------------------------*/
  142. #if _USE_IOCTL != 0
  143. DRESULT disk_ioctl (
  144. BYTE drv, /* Physical drive nmuber (0) */
  145. BYTE ctrl, /* Control code */
  146. void *buff /* Buffer to send/receive data block */
  147. )
  148. {
  149. BYTE n, w, ofs, dl, dh, *ptr = buff;
  150. if (drv) return RES_PARERR;
  151. if (Stat & STA_NOINIT) return RES_NOTRDY;
  152. switch (ctrl) {
  153. case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
  154. printf("disk_ioctl: GET_SECTOR_COUNT\n");
  155. ofs = 60; w = 2; n = 0;
  156. break;
  157. case GET_SECTOR_SIZE : /* Get sectors on the disk (WORD) */
  158. printf("disk_ioctl: GET_SECTOR_SIZE\n");
  159. *(WORD*)buff = 512;
  160. return RES_OK;
  161. case GET_BLOCK_SIZE : /* Get erase block size in sectors (DWORD) */
  162. printf("disk_ioctl: GET_BLOCK_SIZE\n");
  163. *(DWORD*)buff = 32;
  164. return RES_OK;
  165. default:
  166. return RES_PARERR;
  167. }
  168. return RES_OK;
  169. }
  170. #endif /* _USE_IOCTL != 0 */