sd.lst
来自「51环境下的FAT及FAT32文件系统源码」· LST 代码 · 共 1,605 行 · 第 1/5 页
LST
1,605 行
478 =1
479 =1 } BPB_BLOCK32,*pBPB_BLOCK32;
480 =1
481 =1 typedef struct _SYS_INFO_BLOCK{
482 =1 unsigned long StartSector;
483 =1 unsigned long TotalSector;
484 =1 //UINT8 BS_jmpBoo[3];
485 =1 //UINT8 BS_OEMName[8];
486 =1 UINT16 BPB_BytesPerSec;
487 =1 UINT8 BPB_SecPerClus;
488 =1 //UINT8 BPB_RsvdSecCn[2];
489 =1 UINT8 BPB_NumFATs;
490 =1 UINT16 BPB_RootEntCnt;
491 =1 UINT16 BPB_TotSec16;
492 =1 UINT8 BPB_Media;
493 =1 UINT16 BPB_FATSz16;
494 =1 UINT32 BPB_FATSz32;
495 =1 UINT16 BPB_SecPerTrk;
496 =1 UINT16 BPB_NumHeads;
497 =1 unsigned long BPB_HiddSec;
498 =1 unsigned long BPB_TotSec32;
499 =1 UINT8 BS_DrvNum;
500 =1 //UINT8 BS_Reserved1;
501 =1 UINT8 BS_BootSig;
502 =1 UINT8 BS_VolID[4];
503 =1 UINT8 BS_VolLab[11];
504 =1 UINT8 BS_FilSysType[8];
505 =1 //UINT8 ExecutableCode[448];
506 =1 //UINT8 Marker[2];
507 =1 ///////////////////////////////
508 =1 unsigned long FatStartSector;
509 =1 unsigned long RootStartSector;
510 =1 //unsigned long DataStartSector;
511 =1 unsigned long FirstDataSector;
512 =1 //unsigned long FirstSectorofCluster;
513 =1 unsigned long TotCluster;
514 =1 unsigned char bFatType;
515 =1 } SYS_INFO_BLOCK,*pSYS_INFO_BLOCK;
516 =1
517 =1 typedef struct _FILE_INFO{
518 =1 unsigned char bFileOpen;
519 =1 unsigned int StartCluster; // current cluster
520 =1 unsigned long LengthInByte;
521 =1 unsigned int ClusterPointer; // next cluster
522 =1 unsigned long SectorPointer;
523 =1 unsigned int OffsetofSector;
524 =1 unsigned char SectorofCluster;// 0~(sector/cluster-1)
525 =1 unsigned long pointer; // current sector(DSN)
526 =1 // unsigned int FatSectorPointer; // FSN
527 =1 } FILE_INFO, *pFILE_INFO;
528 =1
529 =1 typedef struct _DIR_INFO{
530 =1 unsigned char name[8]; //0
531 =1 unsigned char extension[3]; //9
532 =1 unsigned char attribute; //11
533 =1 unsigned char Reserved[10]; //21
534 =1 unsigned int lastUpdateDate; //23
535 =1 unsigned int lastUpdateTime; //25
536 =1 unsigned int startCluster; //27
537 =1 unsigned long length; //28
538 =1 } DIR_INFO, *pDIR_INFO;
C51 COMPILER V8.02 SD 09/19/2006 15:01:24 PAGE 17
539 =1 /*
540 =1 // Mode Sense
541 =1 */
542 =1 typedef struct _MODE_SENSE_SPC {
543 =1 UINT8 OperationCode; // 1AH
544 =1 UINT8 Reseved0 : 3 ;
545 =1 UINT8 DisableBlockDescriptor : 1 ;
546 =1 UINT8 Reserved0 : 4 ;
547 =1 UINT8 PageCode:6 ;
548 =1 UINT8 PageControl : 2 ;
549 =1 UINT8 Reserved1;
550 =1 UINT8 ParameterLen;
551 =1 UINT8 Control;
552 =1 } MODE_SENSE_SPC, *pMODE_SENSE_SPC;
553 =1
554 =1 /*
555 =1 typedef struct _MODE_PARAMETER_HEAD {
556 =1 UINT8 DataLen;
557 =1 UINT8 MediumType;
558 =1 UINT8 DeviceParameter;
559 =1 UINT8 BlockDescriptorLen;
560 =1 } MODE_PARAMETER_HEAD, *pMODE_PARAMETER_HEAD;
561 =1 */
562 =1 /*
563 =1 // Define Device Capabilities page.
564 =1 */
565 =1 /*
566 =1 typedef struct _MODE_RBC_DEVICE_PARAMETERS_PAGE {
567 =1 UINT8 PageCode : 6;
568 =1 UINT8 Reserved : 1;
569 =1 UINT8 PageSavable : 1;
570 =1 UINT8 PageLength;
571 =1 UINT8 WriteCacheDisable : 1;
572 =1 UINT8 Reserved1 : 7;
573 =1 UINT8 LogicalBlockSize[2];
574 =1 UINT8 NumberOfLogicalBlocks[5];
575 =1 UINT8 PowerPerformance;
576 =1 UINT8 Lockable : 1;
577 =1 UINT8 Formattable : 1;
578 =1 UINT8 Writable : 1;
579 =1 UINT8 Readable : 1;
580 =1 UINT8 Reserved2 : 4;
581 =1 UINT8 Reserved3;
582 =1 }MODE_RBC_DEVICE_PARAMETERS_PAGE, *pMODE_RBC_DEVICE_PARAMETERS_PAGE;
583 =1 */
584 =1 /*
585 =1 // prevent/allow medium removal
586 =1 */
587 =1 typedef struct _MEDIA_REMOVAL_SPC {
588 =1 UINT8 OperationCode; // 1EH
589 =1 UINT8 Reserved0[3];
590 =1 UINT8 Prevent;
591 =1 //UINT8 Reserved1:6 ;
592 =1 //UINT8 Control;
593 =1 } MEDIA_REMOVAL_SPC, *pMEDIA_REMOVAL_SPC;
594 =1
595 =1 /*
596 =1 // Request Sense
597 =1 */
598 =1 typedef struct _REQUEST_SENSE_SPC {
599 =1 UINT8 OperationCode; // 03H
600 =1 UINT8 Reserved[3];
C51 COMPILER V8.02 SD 09/19/2006 15:01:24 PAGE 18
601 =1 UINT8 AllocationLen;
602 =1 UINT8 Control;
603 =1 } REQUEST_SENSE_SPC, *pREQUEST_SENSE_SPC;
604 =1 /*
605 =1 typedef struct _REQUEST_SENSE_DATA {
606 =1 UINT8 ResponseCode : 7;
607 =1 UINT8 Valid : 1;
608 =1
609 =1 UINT8 SegmentNum;
610 =1
611 =1 UINT8 SenseKey : 4;
612 =1 UINT8 Reserved0 : 1;
613 =1 UINT8 WrongLenIndicator : 1;
614 =1 UINT8 EndofMedium : 1;
615 =1 UINT8 FileMark : 1;
616 =1
617 =1 UINT8 Info_0;
618 =1 UINT8 Info_1;
619 =1 UINT8 Info_2;
620 =1 UINT8 Info_3;
621 =1
622 =1 UINT8 AdditionalSenseLen;
623 =1
624 =1 UINT8 CommandSpecInfo_0;
625 =1 UINT8 CommandSpecInfo_1;
626 =1 UINT8 CommandSpecInfo_2;
627 =1 UINT8 CommandSpecInfo_3;
628 =1
629 =1 UINT8 ASC;
630 =1 UINT8 ASCQ;
631 =1 UINT8 FieldReplacableUnitCode;
632 =1 UINT8 SenseKeySpec_0 : 7;
633 =1 UINT8 SenseKeySpecValid : 1;
634 =1 UINT8 SenseKeySpec_1;
635 =1 UINT8 SenseKeySpec_2;
636 =1
637 =1 } REQUEST_SENSE_DATA, *pREQUEST_SENSE_DATA;
638 =1 */
639 =1 /*
640 =1 // Test Unit Ready
641 =1 */
642 =1 typedef struct _TEST_UNIT_SPC {
643 =1 UINT8 OperationCode; // 00H
644 =1 UINT8 Reserved[4];
645 =1 UINT8 Control;
646 =1 } TEST_UNIT_SPC, *pTEST_UNIT_SPC;
647 =1
648 =1 /*
649 =1 // Write Buffer
650 =1 */
651 =1
652 =1 typedef struct _WRITE_BUFFER_SPC {
653 =1 UINT8 OperationCode;
654 =1 UINT8 Mod:4 ;
655 =1 UINT8 Reserved0:4 ;
656 =1 UINT8 BufferID;
657 =1 UINT8 BufferOff_2;
658 =1 UINT8 BufferOff_1;
659 =1 UINT8 BufferOff_0;
660 =1 UINT8 ParameterLen_2;
661 =1 UINT8 ParameterLen_1;
662 =1 UINT8 ParameterLen_0;
C51 COMPILER V8.02 SD 09/19/2006 15:01:24 PAGE 19
663 =1 UINT8 Control;
664 =1 } WRITE_BUFFER_SPC, *pWRITE_BUFFER_SPC;
665 =1
666 =1 typedef union _CDB_RBC {
667 =1 GENERIC_CDB Cdb_Generic;
668 =1
669 =1 // RBC commands
670 =1 GENERIC_RBC RbcCdb_Generic;
671 =1
672 =1 FORMAT_RBC RbcCdb_Format;
673 =1 READ_RBC RbcCdb_Read;
674 =1 READ_CAPACITY_RBC RbcCdb_ReadCapacity;
675 =1 START_STOP_RBC RbcCdb_OnOffUnit;
676 =1 SYNCHRONIZE_CACHE_RBC RbcCdb_SyncCache;
677 =1 VERIFY_RBC RbcCdb_Verify;
678 =1 WRITE_RBC RbcCdb_Write;
679 =1
680 =1
681 =1 // SPC-2 commands
682 =1
683 =1 INQUIRY_SPC SpcCdb_Inquiry;
684 =1 MODE_SELECT_SPC SpcCdb_ModeSelect;
685 =1 MODE_SENSE_SPC SpcCdb_ModeSense;
686 =1 MEDIA_REMOVAL_SPC SpcCdb_Remove;
687 =1 REQUEST_SENSE_SPC SpcCdb_RequestSense;
688 =1 TEST_UNIT_SPC SpcCdb_TestUnit;
689 =1 WRITE_BUFFER_SPC SpcCdb_WriteBuffer;
690 =1
691 =1 // ATAPI Commands
692 =1 READ_10 CmdRead10;
693 =1 WRITE_10 CmdWrite10;
694 =1 MODE_SELECT_10 CmdModeSel10;
695 =1 MODE_SENSE_10 CmdModeSen10;
696 =1 //////////////////////////////////////
697 =1 READ_LONG_CMD SpcCdb_ReadLong;
698 =1
699 =1 } CDB_RBC, *pCDB_RBC;
700 =1
701 =1
9
10 #define BYTE_PER_SEC 512
11 extern SYS_INFO_BLOCK xdata DeviceInfo;
12
13 extern UINT8 xdata DBUF[512]; // defined in the main program
14 extern UINT8 xdata FATBUF[512];
15 extern UINT8 xdata RDIRBUF[512];
16 extern UINT8 xdata respBuf[16];
17
18 unsigned int c_size, c_size_mult, read_bl_len;
19 unsigned long drive_size, maxsect;
20
21 unsigned char GetDevInfo( )
22 {
23 1 unsigned long FATSz;
24 1 unsigned long TotSec;
25 1 unsigned long RootDirSectors;
26 1 unsigned long DataSec;
27 1 unsigned long CountofClusters;
28 1
29 1 pMBR_BLOCK pMBR;
30 1 pBPB_BLOCK pBPB;
31 1 pBPB_BLOCK32 pBPB32;
C51 COMPILER V8.02 SD 09/19/2006 15:01:24 PAGE 20
32 1
33 1 pMBR=(pMBR_BLOCK)FATBUF;
34 1
35 1 DeviceInfo.BPB_BytesPerSec=512; //暂假设为512
36 1
37 1 // Read sector 0 (Partition table)
38 1 if(!SDReadFat(0x0))
39 1 return 0;
40 1 //////////////////////////////////
41 1 SendUart(FATBUF, 512); // debug
42 1
43 1 if(FATBUF[0]==0xeb||FATBUF[0]==0xe9)
44 1 {
45 2 DeviceInfo.StartSector=0;
46 2 }
47 1 else
48 1 {
49 2 //////////////////////////////////
50 2 DeviceInfo.StartSector=SwapINT32(pMBR->StartSector);
51 2 }
52 1 ///////////////////////////////////////////////////////
53 1 DelayUs(5);
54 1 pBPB=(pBPB_BLOCK)FATBUF;
55 1 if(!SDReadFat(DeviceInfo.StartSector))
56 1 return 0;
57 1
58 1 SendUart(FATBUF, 512); // debug
59 1
60 1 DeviceInfo.BPB_BytesPerSec=WordSwap(pBPB->BPB_BytesPerSec);
61 1 DeviceInfo.BPB_SecPerClus=pBPB->BPB_SecPerClus;
62 1 DeviceInfo.BPB_NumFATs=pBPB->BPB_NumFATs;
63 1 DeviceInfo.BPB_RootEntCnt=WordSwap(pBPB->BPB_RootEntCnt);
64 1 DeviceInfo.BPB_TotSec16=WordSwap(pBPB->BPB_TotSec16);
65 1 DeviceInfo.BPB_FATSz16=WordSwap(pBPB->BPB_FATSz16);
66 1 DeviceInfo.BPB_FATSz32=WordSwap(pBPB32->BPB_FATSz32);
67 1 DeviceInfo.BPB_TotSec32=SwapINT32(pBPB->BPB_TotSec32);
68 1 DeviceInfo.FatStartSector=DeviceInfo.StartSector+WordSwap(pBPB->BPB_RsvdSecCnt);
69 1 DeviceInfo.RootStartSector=DeviceInfo.FatStartSector+2*DeviceInfo.BPB_FATSz16;
70 1 DeviceInfo.FirstDataSector=DeviceInfo.RootStartSector+0x20;
71 1 ///////////////////////////////////////////////////////
72 1 // ThisFile.bFileOpen=0;
73 1
74 1 //Judge the FAT type
75 1 if(DeviceInfo.BPB_FATSz16 != 0)
76 1 FATSz = DeviceInfo.BPB_FATSz16;
77 1 else
78 1 FATSz = DeviceInfo.BPB_FATSz32;
79 1
80 1 if(DeviceInfo.BPB_TotSec16 != 0)
81 1 TotSec = DeviceInfo.BPB_TotSec16;
82 1 else
83 1 TotSec = DeviceInfo.BPB_TotSec32;
84 1
85 1 RootDirSectors = ((DeviceInfo.BPB_RootEntCnt * 32) + (DeviceInfo.BPB_BytesPerSec - 1)) / DeviceInfo.BPB_B
-ytesPerSec;
86 1 DataSec = TotSec - (WordSwap(pBPB->BPB_RsvdSecCnt) + (DeviceInfo.BPB_NumFATs * FATSz) + RootDirSectors);
87 1 CountofClusters = DataSec / DeviceInfo.BPB_SecPerClus;
88 1
89 1 if(CountofClusters < 4085)
90 1 {
91 2 DeviceInfo.bFatType = 1; //fat12
92 2 }
C51 COMPILER V8.02 SD 09/19/2006 15:01:24 PAGE 21
93 1 else if(CountofClusters < 65525)
94 1 {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?