1 00:00:00,180 --> 00:00:06,710 With the c module loaded, we can parse the fat16 structure. What we are going to do in the loader 2 00:00:06,710 --> 00:00:12,270 is we are going to find the kernel file and user program. Once we find them, we will load them in the memory 3 00:00:12,270 --> 00:00:14,970 and then we transfer the control to the kernel. 4 00:00:15,630 --> 00:00:21,420 So the file module in the loader includes two basic functions, search file and read file. 5 00:00:22,470 --> 00:00:23,800 OK, let's get started. 6 00:00:24,870 --> 00:00:28,550 As you can see, this is the general structure of fat16 format. 7 00:00:29,340 --> 00:00:34,320 We have bpb bios parameter block, file allocation tables, 8 00:00:35,580 --> 00:00:38,040 root directory and data sections. 9 00:00:38,960 --> 00:00:45,680 The bios parameter block includes information such as bytes per sector, sectors per cluster, 10 00:00:45,830 --> 00:00:47,050 total sectors, etc. 11 00:00:48,060 --> 00:00:54,870 The file allocation table is used to locate the clusters. In the fat16 system, 12 00:00:54,870 --> 00:00:58,140 the cluster is the basic allocation unit which is a set of consecutive sectors. 13 00:00:58,860 --> 00:01:03,030 The sectors per cluster in the BPB will give us this info. 14 00:01:03,980 --> 00:01:09,980 Now let's move to the file allocation table, this table contains the chains of links for a file, 15 00:01:11,150 --> 00:01:17,600 that is, the value in the table indicates the next cluster number. Then you use that value as an index to 16 00:01:17,600 --> 00:01:21,830 find the value in the table which will give you the next cluster number. 17 00:01:22,890 --> 00:01:28,010 To find all the clusters of a file, you continue the process until you reach the end of the file. 18 00:01:28,720 --> 00:01:32,780 The number of the file allocation tables we have in the image is usually 2. 19 00:01:33,390 --> 00:01:36,300 We can get this info in the bios parameter block as well. 20 00:01:37,770 --> 00:01:43,800 The next section is root directory which follows the file allocation tables. We can calculate the size of root directory 21 00:01:43,800 --> 00:01:46,860 with the info in bios parameter block. 22 00:01:47,770 --> 00:01:54,340 In the root directory section, we have root directory entries with each one being 32 bytes. 23 00:01:54,340 --> 00:01:59,040 And these entries represent the files we have in the root directory of the fat16 partition. 24 00:01:59,710 --> 00:02:05,140 If we have files and folders in the root directory, we will see them stored as directory entries here 25 00:02:05,140 --> 00:02:05,500 . 26 00:02:06,190 --> 00:02:11,350 The data section stores the data of the files and folders. If we have subfolders, 27 00:02:11,560 --> 00:02:17,350 the files and folders within the subfolders will be stored as directory entries in the data section. 28 00:02:18,500 --> 00:02:25,190 So the root directory section, as its name implies, stores the files and folders in the root directory. 29 00:02:26,290 --> 00:02:31,600 What we are going to do next is we are going to inspect the fat16 image so that we can see 30 00:02:31,600 --> 00:02:32,970 how to find a file manually. 31 00:02:33,760 --> 00:02:38,410 First off, we mount os image. So we open osfmount 32 00:02:40,880 --> 00:02:42,560 and choose os.img. 33 00:02:47,800 --> 00:02:53,740 Then we uncheck read only drive, because we want to add files in this image and then we click mount. 34 00:02:57,850 --> 00:03:01,160 As you can see right now, the os image is empty. 35 00:03:01,750 --> 00:03:05,230 So here we add the loader folder and kernel.bin file. 36 00:03:08,400 --> 00:03:10,590 Now, we dismount this partition. 37 00:03:12,950 --> 00:03:14,180 So we click dismount. 38 00:03:15,120 --> 00:03:17,520 OK, now we can open the image with hex editor. 39 00:03:19,070 --> 00:03:23,600 If you use Linux or Mac OS, you can inspect the image with hexdump command. 40 00:03:24,710 --> 00:03:31,400 Alright, this is what we got in the hex editor. The first 512 bytes are the mbr code we wrote in the previous videos. 41 00:03:31,400 --> 00:03:36,110 The next few sectors are the loader file 42 00:03:36,110 --> 00:03:37,670 we wrote in the last lecture. 43 00:03:41,080 --> 00:03:47,500 Ok, let’s find the fat16 partition. In the partition entry, we retrieve the starting lba here, 44 00:03:47,530 --> 00:03:51,170 which is 3f 00 00 00. 45 00:03:51,490 --> 00:03:53,810 The data stored in the little-endian order. 46 00:03:53,830 --> 00:03:55,960 So the starting lba is 3f. 47 00:03:55,960 --> 00:04:01,750 We multiply it by 512 which produces the result 7e00. 48 00:04:02,620 --> 00:04:04,630 This is the address of the fat partition. 49 00:04:05,230 --> 00:04:06,400 So we go to 50 00:04:07,630 --> 00:04:09,070 7e00 51 00:04:12,690 --> 00:04:19,769 Now let’s interpret the data with the bios parameter block info. The oem name is free dos 4.1. 52 00:04:21,110 --> 00:04:26,060 The bytes per sector is 200 in hexadecimal which is 512 in decimal. 53 00:04:26,980 --> 00:04:34,160 The sectors per cluster is 4, so each cluster is 2kb. And we have 1 sector reserved. 54 00:04:35,060 --> 00:04:40,420 The next field shows the number of fat tables we have, 2 fat tables in this case. 55 00:04:41,390 --> 00:04:47,450 The root entries represent the number of entries we have in the root directory section, 56 00:04:47,450 --> 00:04:48,920 which is 512 in this example. 57 00:04:49,820 --> 00:04:55,160 The total sectors indicate the number of sectors in this partition. Here we have value 0. 58 00:04:55,370 --> 00:04:58,060 So if it is 0, we will use extended sectors 59 00:04:58,070 --> 00:05:00,620 located at the offset 20. 60 00:05:01,190 --> 00:05:05,530 As you can see, the total sectors is 31f11. 61 00:05:06,350 --> 00:05:07,090 Let's move on. 62 00:05:07,670 --> 00:05:12,220 The media descriptor is f8 which means it’s a fixed media. 63 00:05:13,130 --> 00:05:17,920 The sectors per fat table indicates the number of sectors for a file allocation table, 64 00:05:18,650 --> 00:05:21,580 so we have c8 sectors for each fat table. 65 00:05:22,250 --> 00:05:25,280 OK, we skip the sectors per track and head. 66 00:05:26,250 --> 00:05:31,600 Then we get to hidden sectors which represent how many sectors we have before the partition. 67 00:05:32,190 --> 00:05:36,520 Remember we got the value 3f in the lba field of partition entry. 68 00:05:36,990 --> 00:05:39,480 The value is equal to what we have in this field. 69 00:05:40,330 --> 00:05:47,220 We have talked about the extended sectors. The next field we care about is label. Since we format the partition with the label os, 70 00:05:47,220 --> 00:05:51,360 here we can see os in this field. 71 00:05:52,810 --> 00:05:56,860 The last one is system type. We got fat16 in this field. 72 00:05:57,830 --> 00:06:03,170 OK, with all the information in the bpb, we can locate the fat allocation table 73 00:06:03,170 --> 00:06:09,560 and root directory section. We start off with fat allocation table. To locate the table, 74 00:06:09,590 --> 00:06:15,620 we use the value in the reserved sectors which represent the number of sectors we have 75 00:06:15,620 --> 00:06:18,350 starting from the bpb to the file allocation table. 76 00:06:19,010 --> 00:06:20,410 In this case, we have 1. 77 00:06:20,540 --> 00:06:23,720 So the allocation table is at the next sector. 78 00:06:25,210 --> 00:06:31,700 As you can see, this is the start of fat allocation table. In the fat16 system, 79 00:06:31,810 --> 00:06:34,840 we use 16-bit value for the cluster number. 80 00:06:34,880 --> 00:06:37,050 And note that the first two value are reserved. 81 00:06:37,810 --> 00:06:43,030 Therefore, we start with the third entry in the fat table. We will see how to use this table in a minute. 82 00:06:43,030 --> 00:06:45,660 Following the fat tables, we have root directory section. 83 00:06:45,670 --> 00:06:48,930 To locate the section, 84 00:06:48,980 --> 00:06:55,180 we need the start address of fat table and the size of fat tables. In the bios parameter block, 85 00:06:55,180 --> 00:07:00,730 we know that we have 2 fat tables and each table occupies c8 sectors. 86 00:07:01,450 --> 00:07:08,470 So we add the size of fat tables c8*2 to the start of the fat table 87 00:07:08,470 --> 00:07:08,770 which is sector 1, 88 00:07:09,250 --> 00:07:10,300 we get result 191 sectors. 89 00:07:10,460 --> 00:07:18,600 Then we multiply it by 512, the size is 32200 in hexadecimal 90 00:07:18,730 --> 00:07:20,150 . 91 00:07:20,950 --> 00:07:26,560 This is the offset from the start of the partition. So we add 7e00 to the result. 92 00:07:27,820 --> 00:07:32,080 The root directory section should be at this location. We go to 93 00:07:36,210 --> 00:07:38,580 3a000. 94 00:07:40,920 --> 00:07:47,490 As you can see, we have a couple of root directory entries here. Since each entry is 32 bytes, 95 00:07:47,490 --> 00:07:48,420 we have three entries. 96 00:07:48,930 --> 00:07:51,030 The first one is for the volume label, 97 00:07:51,890 --> 00:07:59,150 we named it os when we format the partition. The next entry is kernel.bin file 98 00:07:59,150 --> 00:08:01,280 and the last one is loader folder we added ourselves. 99 00:08:02,150 --> 00:08:05,660 Ok let’s take a look at the entry structure. In the offset 0, 100 00:08:05,770 --> 00:08:08,120 we have file name which is 8 bytes. 101 00:08:08,660 --> 00:08:11,210 The extension name is stored in the next three bytes. 102 00:08:12,980 --> 00:08:20,360 The label has no extension name. So we only got os. The next entry contains file name and extension name. 103 00:08:21,370 --> 00:08:28,900 Let’s continue. The next byte stores the file attributes. Bit 3 means volume id. So we have 8 in the first entry 104 00:08:28,900 --> 00:08:29,620 . 105 00:08:31,240 --> 00:08:38,320 Bit 4 indicates folder, so we have hexadecimal value 10 in the loader entry. 106 00:08:38,500 --> 00:08:42,280 and we know that loader is a folder. Bit 5 represents archive, 107 00:08:42,610 --> 00:08:44,800 We have 20 in the second entry. 108 00:08:44,800 --> 00:08:46,990 So kernel.bin is a file. 109 00:08:47,890 --> 00:08:54,700 Then we get 10 bytes data reserved. Let’s move on. The next four bytes are for the date and time. 110 00:08:55,240 --> 00:08:57,900 We won’t interpret the data and time in our system. 111 00:08:58,510 --> 00:09:04,060 Let's continue. The starting cluster is the info we need to locate the file data in the data section. 112 00:09:04,480 --> 00:09:09,590 It’s two bytes data. The last 4 bytes are used to store the file size. 113 00:09:10,300 --> 00:09:10,760 Alright, 114 00:09:10,810 --> 00:09:12,670 that's it for the directory entries. 115 00:09:13,220 --> 00:09:16,910 Let’s see how to find the file data with the entry. 116 00:09:17,110 --> 00:09:20,050 To do it, we use the starting cluster, 2 in this case. 117 00:09:21,000 --> 00:09:26,130 and you can see this is the file size and the result is file size in bytes. 118 00:09:29,190 --> 00:09:35,220 Note that the starting cluster number for the data section is 2. 119 00:09:35,220 --> 00:09:37,950 Therefore, we will subtract 2 from the starting cluster of the entry to get the correct number. 120 00:09:38,520 --> 00:09:42,140 In this example, it’s 0 which is the start of the data section. 121 00:09:42,990 --> 00:09:45,650 The data section follows the root directory section. 122 00:09:45,690 --> 00:09:51,180 We have calculated the address of root directory section which is 3a000 in this example 123 00:09:51,180 --> 00:09:51,850 . 124 00:09:52,500 --> 00:09:56,730 The size of root directory section is the number of entries * 32 125 00:09:56,730 --> 00:09:57,290 . 126 00:09:58,080 --> 00:10:02,190 We know that we have 512 entries in the bios parameter block, 127 00:10:02,400 --> 00:10:05,300 so the size of the section is 512*32 128 00:10:05,340 --> 00:10:09,420 equals 4000 in hexadecimal. 129 00:10:10,440 --> 00:10:16,440 We add it to 3a000 and address is 3e000. 130 00:10:17,320 --> 00:10:19,330 And this is the address we are looking for. 131 00:10:20,650 --> 00:10:21,670 So we go to 132 00:10:25,560 --> 00:10:27,120 3e000 133 00:10:29,490 --> 00:10:31,470 You can see we have lots of data here. 134 00:10:35,340 --> 00:10:41,370 We get one cluster of data for this file, which is 2kb of data, how do we find the following 135 00:10:41,370 --> 00:10:44,550 data since the size of the file is more than 2kb? 136 00:10:45,630 --> 00:10:51,600 To do it, we find the next cluster using fat table. Remember the starting cluster number is 2. 137 00:10:51,600 --> 00:10:56,240 So we use 2 as an index to locate the item in the table. T 138 00:10:57,260 --> 00:10:59,000 So let's go back to the fat table. 139 00:11:10,920 --> 00:11:17,730 You can see at the index 2 of the table, we get index 3, then we use 3 as an index into this table 140 00:11:17,730 --> 00:11:18,150 . 141 00:11:19,100 --> 00:11:20,630 and we get the value 4. 142 00:11:21,480 --> 00:11:24,540 Next we use 4 as an index to do the same process. 143 00:11:25,740 --> 00:11:32,340 As you see, in this example, we get consecutive numbers. We continue doing it until we reach the end of the sectors 144 00:11:32,340 --> 00:11:36,060 which is the value ffff. 145 00:11:37,640 --> 00:11:40,730 now we collect all the clusters for the file data. 146 00:11:41,670 --> 00:11:44,130 And all the data is stored in these clusters. 147 00:11:45,490 --> 00:11:52,120 To get the data using the clusters, we use the formula, as we have talked about, 148 00:11:52,120 --> 00:11:58,000 the start address of data section + the cluster number – 2 and then times the size of the cluster. 149 00:11:59,340 --> 00:12:02,130 If you like, you can check the data in those clusters. 150 00:12:03,580 --> 00:12:10,440 As for the subfolders, the data of the subfolders is the 32-byte directory entries 151 00:12:10,450 --> 00:12:11,590 as we saw in the root directory section. 152 00:12:12,540 --> 00:12:17,960 The difference is that they are stored in the data section. For example, in the root directory entry, 153 00:12:23,930 --> 00:12:27,800 we have one folder loader. The file size is 0 154 00:12:27,830 --> 00:12:29,090 if this is a directory. 155 00:12:30,060 --> 00:12:32,400 And the starting cluster is c. 156 00:12:33,940 --> 00:12:41,350 ok we use the formula to locate its data. the data section plus the cluster number c – 2 157 00:12:41,350 --> 00:12:42,490 times the size of clusters. 158 00:12:43,580 --> 00:12:50,150 The data section is at 3e000 and we add it to the result 5000 in hexadecimal. 159 00:12:51,560 --> 00:12:52,520 So we go to 160 00:12:55,470 --> 00:12:56,670 43000. 161 00:13:01,190 --> 00:13:11,360 Now you can see we have 13 entries build.h debug.c etc, which are the files in the loader folder. 162 00:13:12,210 --> 00:13:14,010 Also we have 2 more entries. 163 00:13:15,040 --> 00:13:16,570 The . and .. 164 00:13:17,810 --> 00:13:22,460 The . is pointing to itself. And .. is pointing to its parent directory. 165 00:13:23,970 --> 00:13:26,220 Alright, that’s it for inspecting the image. 166 00:13:27,220 --> 00:13:29,370 In the next video, we will start building the file module. 167 00:13:29,410 --> 00:13:31,330 See you in the next lecture.