Operating System
DBR.c
Go to the documentation of this file.
1 
8 #include <stdint.h>
9 #include "fsutilities.h"
10 #include "FATMacro.h"
11 
12 #include "filesystem.h"
13 
14 uint16_t DBRkernelLoader() {
15  void* bpb = (void*)(BPB_ADDRESS);
16  uint16_t sectorPerCluster = *(uint8_t*)(bpb+2);
17  uint16_t reservedSector = *(uint16_t*)(bpb+3);
18  uint16_t numberOfFAT = *(uint8_t*)(bpb + 5);
19  uint16_t sectorPerFAT = *(uint8_t*)(bpb + 11);
20  uint16_t hiddenSector = *(uint8_t*)(bpb + 17);
21  uint16_t dataBlockBase = hiddenSector + reservedSector
22  + numberOfFAT * sectorPerFAT
23  + 1;
24  uint16_t rootSectorNth = dataBlockBase + 2 * sectorPerCluster;
25 
26  // load FAT table into memory 0x8200
27  uint16_t FATTableNth = hiddenSector + reservedSector + 1;
28  loadLogicSector(FATTableNth, FAT_TABLE_ADDRESS, 2);
29  FAT_ITEM_T* FAT_table = (FAT_ITEM_T*)(FAT_TABLE_ADDRESS);
30  // load datablock: root area into memory 0x8600
31  loadLogicSector(rootSectorNth, ROOT_AREA_ADDRESS, 1);
32 
33  // WARNING, IMPORTANT: if you change cluster into 2 sectors
34  // you may also want to change the defination below:
35  // loadLogicSector(.., .. 1) 1 -> sectorPerCluster
36  // macro filesize2sectors() func-name -> filesize2clusters()
37  FAT_ITEM* pRootEntities = (FAT_ITEM*)(ROOT_AREA_ADDRESS);
38  FAT_ITEM_T* FATRootItem = FAT_table + 4;
39  for (int16_t i = 0; i < 3; ++i) {
40  FAT_ITEM* p = pRootEntities + i;
41  if (__fs_strcmp(p->filename, "kernel") == 0) {
42  uint16_t cluster = p->blow_cluster;
43  uint16_t numOfSectors = filesize2sectors(p->filesize);
44  uint16_t kernelSectorNth = dataBlockBase + (cluster * sectorPerCluster);
45  for (int16_t j = 0; j < numOfSectors; ++j) {
46  loadLogicSector(kernelSectorNth, KERNEL_ADDRESS + j * 0x200, 1);
47  cluster = *FATRootItem;
48  FATRootItem = FAT_table + cluster;
49  kernelSectorNth = dataBlockBase + (cluster * sectorPerCluster);
50  }
51  break;
52  }
53  }
54  __asm__("jmpl $0, $" TO_STRING(KERNEL_ADDRESS) "\n");
55  return 0;
56 }
57 
static void loadLogicSector(uint16_t lgsector, uint16_t addr, uint16_t num)
Definition: fsutilities.h:8
uint16_t FAT_ITEM_T
Definition: FATMacro.h:9
#define KERNEL_ADDRESS
Definition: filesystem.h:20
uint16_t DBRkernelLoader()
Definition: DBR.c:14
static int16_t __fs_strcmp(const char *s1, const char *s2)
Definition: fsutilities.h:20
#define BPB_ADDRESS
Definition: filesystem.h:16
uint32_t filesize
in bytes
Definition: FATMacro.h:36
#define ROOT_AREA_ADDRESS
Definition: filesystem.h:18
__asm__(".globl _start\)
#define TO_STRING(X)
Definition: filesystem.h:23
uint8_t filename[8]
Definition: FATMacro.h:22
#define FAT_TABLE_ADDRESS
Definition: filesystem.h:17
uint16_t blow_cluster
Definition: FATMacro.h:35
all the macro needed by FAT inner implementation
#define filesize2sectors(X)
Definition: fsutilities.h:15