FAT16.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /******************************************************************************
  2. *
  3. * (c) copyright Freescale Semiconductor 2008
  4. * ALL RIGHTS RESERVED
  5. *
  6. * File Name: FAT16.c
  7. *
  8. * Purpose: This file is for a USB Mass-Storage Device bootloader. This file
  9. * mimics a FAT16 drive in order to enumerate as a disk drive
  10. *
  11. * Assembler: Codewarrior for Microcontrollers V6.2
  12. *
  13. * Version: 1.3
  14. *
  15. *
  16. * Author: Derek Snell
  17. *
  18. * Location: Indianapolis, IN. USA
  19. *
  20. * UPDATED HISTORY:
  21. *
  22. * REV YYYY.MM.DD AUTHOR DESCRIPTION OF CHANGE
  23. * --- ---------- ------ ---------------------
  24. * 1.3 2009.01.13 Derek Snell Added linker SEGMENTs for S08 version
  25. * 1.2 2008.11.24 Derek Snell Added Volume label "BOOTLOADER" to FAT16 root directory
  26. * 1.1 2008.09.17 Derek Snell Updated to give S19 address error in status
  27. * 1.0 2008.06.10 Derek Snell Initial version
  28. *
  29. *
  30. ******************************************************************************/
  31. /* Freescale is not obligated to provide any support, upgrades or new */
  32. /* releases of the Software. Freescale may make changes to the Software at */
  33. /* any time, without any obligation to notify or provide updated versions of */
  34. /* the Software to you. Freescale expressly disclaims any warranty for the */
  35. /* Software. The Software is provided as is, without warranty of any kind, */
  36. /* either express or implied, including, without limitation, the implied */
  37. /* warranties of merchantability, fitness for a particular purpose, or */
  38. /* non-infringement. You assume the entire risk arising out of the use or */
  39. /* performance of the Software, or any systems you design using the software */
  40. /* (if any). Nothing may be construed as a warranty or representation by */
  41. /* Freescale that the Software or any derivative work developed with or */
  42. /* incorporating the Software will be free from infringement of the */
  43. /* intellectual property rights of third parties. In no event will Freescale */
  44. /* be liable, whether in contract, tort, or otherwise, for any incidental, */
  45. /* special, indirect, consequential or punitive damages, including, but not */
  46. /* limited to, damages for any loss of use, loss of time, inconvenience, */
  47. /* commercial loss, or lost profits, savings, or revenues to the full extent */
  48. /* such may be disclaimed by law. The Software is not fault tolerant and is */
  49. /* not designed, manufactured or intended by Freescale for incorporation */
  50. /* into products intended for use or resale in on-line control equipment in */
  51. /* hazardous, dangerous to life or potentially life-threatening environments */
  52. /* requiring fail-safe performance, such as in the operation of nuclear */
  53. /* facilities, aircraft navigation or communication systems, air traffic */
  54. /* control, direct life support machines or weapons systems, in which the */
  55. /* failure of products could lead directly to death, personal injury or */
  56. /* severe physical or environmental damage (High Risk Activities). You */
  57. /* specifically represent and warrant that you will not use the Software or */
  58. /* any derivative work of the Software for High Risk Activities. */
  59. /* Freescale and the Freescale logos are registered trademarks of Freescale */
  60. /* Semiconductor Inc. */
  61. /*****************************************************************************/
  62. #include "derivative.h"
  63. #include "FAT16.h"
  64. #include "disk.h"
  65. static unsigned char GetASCIIValue(unsigned char value);
  66. /*****************************************************************************
  67. * External references.
  68. *****************************************************************************/
  69. extern unsigned char BootloaderStatus;
  70. extern uint_32 S19Address;
  71. /********************************************************************
  72. *********************************************************************
  73. * FAT16 Boot Sector
  74. *********************************************************************
  75. ********************************************************************/
  76. const uint_8 FAT16_BootSector[FATBootSize]=
  77. {
  78. 0xEB, /*00 - BS_jmpBoot */
  79. 0x3C, /*01 - BS_jmpBoot */
  80. 0x90, /*02 - BS_jmpBoot */
  81. 'M','S','D','O','S','5','.','0', /* 03-10 - BS_OEMName */
  82. 0x00, /*11 - BPB_BytesPerSec = 512 */
  83. 0x02, /*11 - BPB_BytesPerSec = 512 */
  84. 32, /*13 - BPB_Sec_PerClus = 32 */
  85. 2, /*14 - BPB_RsvdSecCnt = 2 */
  86. 0, /*15 - BPB_RsvdSecCnt = 2 */
  87. 2, /*16 - BPB_NumFATs = 2 */
  88. 0x0, /*17 - BPB_RootEntCnt = 512 */
  89. 0x2, /*18 - BPB_RootEntCnt = 512 */
  90. 0, /*19 - BPB_TotSec16 = 0 */
  91. 0, /*20 - BPB_TotSec16 = 0 */
  92. 0xF8, /*21 - BPB_Media = 0xF8 */
  93. 243, /*22 - BPBFATSz16 = 243 */
  94. 0, /*23 - BPBFATSz16 = 243 */
  95. 63, /*24 - BPB_SecPerTrk = 63 */
  96. 0, /*25 - BPB_SecPerTrk = 63 */
  97. 255, /*26 - BPB_NumHeads = 255 */
  98. 0, /*27 - BPB_NumHeads = 255 */
  99. 249, /*28 - BPB_HiddSec = 249 */
  100. 0, /*29 - BPB_HiddSec = 249 */
  101. 0, /*30 - BPB_HiddSec = 249 */
  102. 0, /*31 - BPB_HiddSec = 249 */
  103. 0x07, /*32 - BPB_TotSec32 = 1,983,495 */
  104. 0x44, /*33 - BPB_TotSec32 = 1,983,495 */
  105. 0x1E, /*34 - BPB_TotSec32 = 1,983,495 */
  106. 0x00, /*35 - BPB_TotSec32 = 1,983,495 */
  107. 0, /*36 - BS_DrvNum = 1 */
  108. 1, /*37 - BS_Reserved1 = 1 */
  109. 0x29, /*38 - BS_BootSig = 0x29 */
  110. 0xBd, /*39 - BS_VolID = 0x02DDA5BD */
  111. 0xA5, /*40 - BS_VolID = 0x02DDA5BD */
  112. 0xDD, /*41 - BS_VolID = 0x02DDA5BD */
  113. 0x02, /*42 - BS_VolID = 0x02DDA5BD */
  114. 'N','O',' ','N','A','M','E',' ',' ',' ',' ', /*43-53 - BS_VolLab */
  115. 'F','A','T','1','6',' ',' ',' ' /*54-61 - BS_FilSysType */
  116. };
  117. /********************************************************************
  118. *********************************************************************
  119. * First Sector of FAT Table
  120. *********************************************************************
  121. ********************************************************************/
  122. const uint_8 FAT16_TableSector0[FATTableSize]=
  123. {
  124. 0xF8,0xFF,0xFF,0x7F
  125. };
  126. /********************************************************************
  127. *********************************************************************
  128. * FAT Root Directory Sector
  129. *********************************************************************
  130. ********************************************************************/
  131. const uint_8 FAT16_ReadyFileName[FATFileNameSize]=
  132. {
  133. 'R','E','A','D','Y',' ',' ',' ','T','X','T' /*00-10 - Short File Name */
  134. };
  135. const uint_8 FAT16_SuccessFileName[FATFileNameSize]=
  136. {
  137. 'S','U','C','C','E','S','S',' ','T','X','T' /*00-10 - Short File Name */
  138. };
  139. const uint_8 FAT16_FlashFailFileName[FATFileNameSize]=
  140. {
  141. 'F','F','A','I','L','E','D',' ','T','X','T' /*00-10 - Short File Name */
  142. };
  143. const uint_8 FAT16_StartedFileName[FATFileNameSize]=
  144. {
  145. 'S','T','A','R','T','E','D',' ','T','X','T' /*00-10 - Short File Name */
  146. };
  147. const uint_8 FAT16_RootDirSector[FATDirSize]=
  148. {
  149. 0x20, /*11 - Archive Attribute set */
  150. 0x00, /*12 - Reserved */
  151. 0x4B, /*13 - Create Time Tenth */
  152. 0x9C, /*14 - Create Time */
  153. 0x42, /*15 - Create Time */
  154. 0x92, /*16 - Create Date */
  155. 0x38, /*17 - Create Date */
  156. 0x92, /*18 - Last Access Date */
  157. 0x38, /*19 - Last Access Date */
  158. 0x00, /*20 - Not used in FAT16 */
  159. 0x00, /*21 - Not used in FAT16 */
  160. 0x9D, /*22 - Write Time */
  161. 0x42, /*23 - Write Time */
  162. 0x92, /*24 - Write Date */
  163. 0x38, /*25 - Write Date */
  164. 0x00, /*26 - First Cluster (none, because file is empty) */
  165. 0x00, /*27 - First Cluster (none, because file is empty) */
  166. 0x00, /*28 - File Size */
  167. 0x00, /*29 - File Size */
  168. 0x00, /*30 - File Size */
  169. 0x00, /*31 - File Size */
  170. 'S','D',' ','L','O','A','D','E','R',' ',' ', /*32-42 - Volume label */
  171. 0x08, /*43 - File attribute = Volume label */
  172. 0x00, /*44 - Reserved */
  173. 0x00, /*45 - Create Time Tenth */
  174. 0x00, /*46 - Create Time */
  175. 0x00, /*47 - Create Time */
  176. 0x00, /*48 - Create Date */
  177. 0x00, /*49 - Create Date */
  178. 0x00, /*50 - Last Access Date */
  179. 0x00, /*51 - Last Access Date */
  180. 0x00, /*52 - Not used in FAT16 */
  181. 0x00, /*53 - Not used in FAT16 */
  182. 0x9D, /*54 - Write Time */
  183. 0x42, /*55 - Write Time */
  184. 0x92, /*56 - Write Date */
  185. 0x38, /*57 - Write Date */
  186. };
  187. /*********************************************************
  188. * Name: GetASCIIValue
  189. *
  190. * Desc: Converts hex value to ASCII character
  191. *
  192. * Parameter: hex value to convert
  193. *
  194. * Return: unsigned char, ASCII character
  195. *
  196. **********************************************************/
  197. static unsigned char GetASCIIValue
  198. (
  199. unsigned char value
  200. )
  201. {
  202. /* Body */
  203. if(value <= 9)
  204. {
  205. return (uint_8)(value + '0');
  206. } else if(value <= 0xF)
  207. {
  208. return (uint_8)(value - 0xA + 'A');
  209. } else
  210. return 'Z';
  211. } /* EndBody */
  212. /*********************************************************
  213. * Name: FATReadLBA
  214. *
  215. * Desc: Read a Logical Block Address
  216. *
  217. * Parameter: FAT_LBA - Logical Block Address to Read
  218. * pu8DataPointer - Pointer to array to store data read
  219. *
  220. * Return: None
  221. *
  222. **********************************************************/
  223. void FATReadLBA
  224. (
  225. uint_32 FAT_LBA,
  226. uint_8 *pu8DataPointer
  227. )
  228. {
  229. /* Body */
  230. int_32 i;
  231. switch (FAT_LBA)
  232. {
  233. /* Boot Sector */
  234. case FATBootSec:
  235. /* Write Boot Sector info */
  236. for(i=0;i<FATBootSize;i++)
  237. {
  238. *pu8DataPointer++ = FAT16_BootSector[i];
  239. } /* EndFor */
  240. /* Rest of sector empty except last two bytes */
  241. i += 2;
  242. while (i++ < FATBytesPerSec)
  243. {
  244. *pu8DataPointer++ = 0;
  245. } /* EndWhile */
  246. /* Boot Sector requires these 2 bytes at end */
  247. *pu8DataPointer++ = 0x55;
  248. *pu8DataPointer++ = 0xAA;
  249. break;
  250. /* FAT Table Sector */
  251. case FATTable0Sec0:
  252. case FATTable1Sec0:
  253. /* Write FAT Table Sector */
  254. for(i=0;i<FATTableSize;i++)
  255. {
  256. *pu8DataPointer++ = FAT16_TableSector0[i];
  257. } /* EndFor */
  258. /* Rest of sector empty */
  259. while (i++ < FATBytesPerSec)
  260. {
  261. *pu8DataPointer++ = 0;
  262. } /*ENdWhile */
  263. break;
  264. /* Root Directory Sector */
  265. case FATRootDirSec0:
  266. /* Write file name */
  267. switch (BootloaderStatus)
  268. {
  269. case BootloaderReady:
  270. default:
  271. for(i=0;i<FATFileNameSize;i++)
  272. {
  273. *pu8DataPointer++ = FAT16_ReadyFileName[i];
  274. } /* EndFor */
  275. break;
  276. case BootloaderFlashError:
  277. for(i=0;i<FATFileNameSize;i++)
  278. {
  279. *pu8DataPointer++ = FAT16_FlashFailFileName[i];
  280. } /* EndFor */
  281. break;
  282. case BootloaderSuccess:
  283. for(i=0;i<FATFileNameSize;i++) {
  284. *pu8DataPointer++ = FAT16_SuccessFileName[i];
  285. } /* EndFor */
  286. break;
  287. case BootloaderStarted:
  288. for(i=0;i<FATFileNameSize;i++) {
  289. *pu8DataPointer++ = FAT16_StartedFileName[i];
  290. }
  291. break;
  292. } /* EndSwitch */
  293. /* Write rest of file FAT structure */
  294. for(i=0;i<FATDirSize;i++)
  295. {
  296. *pu8DataPointer++ = FAT16_RootDirSector[i];
  297. } /* EndFor */
  298. /* Rest of sector empty to signify no more files */
  299. i += FATFileNameSize;
  300. while (i++ < FATBytesPerSec)
  301. {
  302. *pu8DataPointer++ = 0;
  303. } /* EndWhile */
  304. break;
  305. /* All other sectors empty */
  306. default:
  307. i = 0;
  308. while (i++ < FATBytesPerSec)
  309. {
  310. *pu8DataPointer++ = 0;
  311. } /* EndWhile */
  312. break;
  313. } /* EndSwitch */
  314. } /* EndBody */
  315. /* EOF */