1 00:00:00,940 --> 00:00:07,120 In this lecture, we will edit the loader file to load the image in the memory. Before we delve into the detail 2 00:00:07,120 --> 00:00:07,640 , 3 00:00:07,660 --> 00:00:10,510 let’s take a look at the boot process of the system. 4 00:00:11,320 --> 00:00:16,900 As you can see, what it’s showing in the slide is the boot process in the previous lectures when we don’t implement the file system 5 00:00:16,900 --> 00:00:18,460 . 6 00:00:19,870 --> 00:00:24,890 In this section, we can write the boot code and loader file into the image as we did before. 7 00:00:25,650 --> 00:00:32,020 The difference is that the kernel file and user files can be stored in the fat16 image as normal files, 8 00:00:32,680 --> 00:00:35,700 the loader will find and load the kernel in the memory. 9 00:00:36,250 --> 00:00:41,060 Once the kernel is loaded, we can load the user files with the help of the file system. 10 00:00:41,860 --> 00:00:46,480 Therefore, when we finish the boot file and loader file, write them into the image, 11 00:00:46,660 --> 00:00:51,820 We don't need to write the kernel file each time we complete the kernel. 12 00:00:51,860 --> 00:00:56,470 Instead, we can copy the kernel to the image just as we copy the normal files in the fat16 partition 13 00:00:56,470 --> 00:00:57,070 . 14 00:00:58,350 --> 00:00:59,500 Now, let's get started. 15 00:01:01,820 --> 00:01:08,300 In the boot folder, we create a new folder called loader where we add more stuff in it 16 00:01:08,310 --> 00:01:09,830 with assembly and c files. 17 00:01:11,720 --> 00:01:18,170 The assembly file is responsible for loading the image and c module which is a new module is used for loading the kernel 18 00:01:18,170 --> 00:01:18,860 . 19 00:01:19,780 --> 00:01:21,550 We start off with the assembly file. 20 00:01:22,380 --> 00:01:28,920 As you can see loader file is moved into the loader folder. We made some changes in this file in order to load the fat16 image 21 00:01:28,920 --> 00:01:29,520 . 22 00:01:30,710 --> 00:01:32,390 So let's open the loader file. 23 00:01:34,390 --> 00:01:41,240 The first change we made is we removed load kernel and user parts. 24 00:01:41,240 --> 00:01:46,510 Because the kernel and user programs are now stored in the fat16 image, 25 00:01:46,510 --> 00:01:50,320 we will move this part to the c module of the loader which can read the file from the image. 26 00:01:51,190 --> 00:01:51,490 Alright, 27 00:01:51,550 --> 00:01:53,410 let's move on. 28 00:01:53,440 --> 00:01:55,980 In the slide, we know that the memory location where we will load the image 29 00:01:55,990 --> 00:01:59,220 is set to higher part of 1g region. 30 00:01:59,860 --> 00:02:03,370 Remember in the system, we only use the lower 1g of ram. 31 00:02:03,790 --> 00:02:07,780 So the file system is located in the higher part of this region. 32 00:02:08,960 --> 00:02:14,180 Another thing we need to do before we read the image is that we should make sure that the memory at this location 33 00:02:14,180 --> 00:02:18,780 is the free region we can use. To perform the check, 34 00:02:18,800 --> 00:02:20,570 we use the memory map info. 35 00:02:21,630 --> 00:02:25,640 Here is code of retrieving the memory map by the bios service. 36 00:02:26,980 --> 00:02:33,550 Because the loader file in this example is larger than the one in previous lectures, 37 00:02:33,690 --> 00:02:35,280 it could be located in the address 9000 and above. 38 00:02:36,160 --> 00:02:42,370 So we change the destination of memory info block from 9000 to 20000. 39 00:02:43,950 --> 00:02:47,830 In this case, we access the memory using es register. 40 00:02:48,390 --> 00:02:54,180 First off, we copy the value 2000 to es and change the address 9000 to es 0 and change the address 9000 to 41 00:02:54,180 --> 00:02:55,850 to es:0. 42 00:02:56,850 --> 00:03:03,990 So the segment part of the address is 2000 and offset is 0. The physical address it points to 43 00:03:04,380 --> 00:03:05,190 is address 20000 44 00:03:05,200 --> 00:03:06,030 . 45 00:03:07,950 --> 00:03:13,230 The offset part of base address of memory info block is changed to 8. 46 00:03:13,830 --> 00:03:17,130 In this case, es di is pointing to the address 20008 47 00:03:17,370 --> 00:03:20,570 which is the destination address. 48 00:03:21,550 --> 00:03:26,710 OK, at this point, we can check each block of memory info to see if we can load the image. 49 00:03:27,640 --> 00:03:29,590 Once we get a block of data here, 50 00:03:32,440 --> 00:03:37,990 we do the test. The structure is discussed in detail in section memory management. 51 00:03:39,110 --> 00:03:46,370 The first check we will perform is the memory type, we only find the memory region with type 1 free memory. 52 00:03:46,910 --> 00:03:53,590 The memory type is the third field in the structure which is at offset 16. If it’s not equal to 1, 53 00:03:53,600 --> 00:03:54,620 we jump to continue. 54 00:03:55,310 --> 00:03:57,050 Otherwise, it's a free region. 55 00:03:57,060 --> 00:04:01,250 We go ahead and check if the memory region includes this range. 56 00:04:02,330 --> 00:04:08,060 The first member of the structure is the base address which is 8 bytes long. Here we check the higher part of the base address, 57 00:04:08,060 --> 00:04:14,570 if it is nonzero, it means that the base address of this region is larger than 4gb 58 00:04:14,570 --> 00:04:17,850 which means it’s not the region we want, 59 00:04:18,290 --> 00:04:20,029 we simply jump to continue. 60 00:04:22,120 --> 00:04:30,100 If this check passed, we move the lower part of the address to eax, and compare it with the address. 61 00:04:30,100 --> 00:04:32,350 If it’s larger than this address, we also jump to continue. 62 00:04:33,850 --> 00:04:37,210 Otherwise we will compare the higher part of the length with 0. 63 00:04:38,210 --> 00:04:43,880 The length is also 8 bytes data. So if the higher part is nonzero, it means that 64 00:04:43,880 --> 00:04:46,010 the length of this region is larger than 4gb 65 00:04:46,880 --> 00:04:50,960 and this is the region where we can load the image. So we jump to find. 66 00:04:52,740 --> 00:04:58,300 Otherwise, we add the lower part of the length to the base address and then compare it with the address plus the size of image 67 00:04:58,350 --> 00:05:02,010 100mb in this case. 68 00:05:03,090 --> 00:05:06,600 If eax is less than the value, we will jump to continue, 69 00:05:07,780 --> 00:05:10,140 otherwise this is the region we are looking for. 70 00:05:12,170 --> 00:05:20,450 Here we define a variable load image and initialize it with 0. If we find the region, 71 00:05:20,450 --> 00:05:21,440 we set the value to 1. 72 00:05:23,610 --> 00:05:30,180 After get memory done, we check the value of load image. If it’s 1, then we can move on. 73 00:05:30,720 --> 00:05:36,210 If it’s not 1, we jump to read error and stop the system because we cannot load the fat16 image 74 00:05:36,210 --> 00:05:37,560 . 75 00:05:39,260 --> 00:05:46,280 OK, now we can load fat16 image. Loading fat16 image in this example is not an easy task to do. 76 00:05:46,280 --> 00:05:46,540 . 77 00:05:47,030 --> 00:05:49,770 Here is the problem we will face in the real mode. 78 00:05:50,600 --> 00:05:56,330 Remember we can only access 1mb of memory in real mode 79 00:05:56,480 --> 00:05:58,380 and the fat16 image in this example is 100mb. 80 00:05:59,270 --> 00:06:02,450 How can we load the entire image into memory in the real mode. 81 00:06:03,260 --> 00:06:06,050 The method we choose is using big real mode. 82 00:06:06,560 --> 00:06:07,670 In the big real mode, 83 00:06:07,700 --> 00:06:11,350 we can access 4gb of memory just like in the protected mode. 84 00:06:12,340 --> 00:06:18,150 To do it, we just modify the segment limits in the hidden part of the segment register. 85 00:06:18,160 --> 00:06:24,470 Remember we have talked about it in the lecture long mode. When we load a segment descriptor into the segment register 86 00:06:24,470 --> 00:06:25,090 , 87 00:06:25,510 --> 00:06:29,480 the descriptor value will be loaded into the hidden part of the segment register 88 00:06:29,830 --> 00:06:31,600 and then the value becomes the valid value. 89 00:06:31,600 --> 00:06:34,500 In the real mode, the segment limits is 1mb 90 00:06:34,510 --> 00:06:39,370 and what we will do in this case is switch to protected mode 91 00:06:40,940 --> 00:06:47,000 and load a descriptor with the segment limits being 4gb, once the descriptor is loaded, 92 00:06:47,000 --> 00:06:48,960 the segment limits will be change to 4gb. 93 00:06:49,580 --> 00:06:51,140 Then we switch back to real mode, 94 00:06:51,500 --> 00:06:57,950 at this point, we can access 4gb of memory using this segment register since the limits is 4gb 95 00:06:57,950 --> 00:06:59,930 instead of 1mb. 96 00:07:00,860 --> 00:07:02,660 It sounds a little bit complex. 97 00:07:02,690 --> 00:07:07,160 let’s check it out in the bochs debugger so that you can see it in action. 98 00:07:08,210 --> 00:07:11,900 In bochs, we have what it’s called magic break point. 99 00:07:13,680 --> 00:07:15,540 In the configuration file, 100 00:07:20,250 --> 00:07:23,250 we add magic break enable equals 1. 101 00:07:24,490 --> 00:07:31,420 After we enable magic point in the box configuration file, we can set a break point in the code 102 00:07:32,350 --> 00:07:35,470 by adding instruction xchg bx bx. 103 00:07:36,480 --> 00:07:41,850 When we run the code in the debugger, it will automatically stop here. So let's do a test. 104 00:07:43,020 --> 00:07:46,480 For example, we add xchg bx bx 105 00:07:48,760 --> 00:07:51,000 we built the project and see the result. 106 00:07:54,620 --> 00:07:56,240 In the build script of the loader, 107 00:07:58,500 --> 00:08:01,260 We only built the loader file to test it out. 108 00:08:02,290 --> 00:08:05,200 In the terminal, we run the build script. 109 00:08:10,500 --> 00:08:15,350 To run the debugger, we right click bochs configuration file and choose debugger, 110 00:08:17,380 --> 00:08:18,520 then we click start. 111 00:08:22,730 --> 00:08:25,580 In the command prompt we type c to continue. 112 00:08:28,630 --> 00:08:34,350 Now you can see bochs stops at instruction mov ax 3. If we check it out, 113 00:08:35,380 --> 00:08:41,700 in the loader file, you can mov ax 3 is right after xchg bx bx. 114 00:08:46,250 --> 00:08:53,420 Ok let’s examine the registers. To check the general-purpose registers, we type reg and press enter. 115 00:08:54,860 --> 00:09:00,990 And the value of general-purpose registers is showing here, what do we want here is segment register 116 00:09:01,010 --> 00:09:04,310 So we type sreg and press enter. 117 00:09:06,540 --> 00:09:10,330 here shows all the segment registers with limits being 1mb. 118 00:09:10,980 --> 00:09:14,280 Therefore we cannot access memory above the 1mb limits. 119 00:09:15,270 --> 00:09:16,680 We type q to exit it out. 120 00:09:18,650 --> 00:09:23,500 With that in mind, we know what the problem is and now let's change the limits of the segment register 121 00:09:23,510 --> 00:09:24,080 . 122 00:09:26,660 --> 00:09:30,230 The segment register we choose is fs register. 123 00:09:31,180 --> 00:09:38,290 So the process is like this, we switch to protected mode, load the fs register with a new descriptor 124 00:09:38,290 --> 00:09:39,640 and then switch back to real mode. 125 00:09:42,640 --> 00:09:47,020 The code here is switch to protected mode as we have talked about before. 126 00:09:47,940 --> 00:09:49,880 Note that we don’t load idt 127 00:09:50,900 --> 00:09:54,650 because we still need the bios services when we are back to real mode. 128 00:09:55,970 --> 00:10:02,150 Once we enable protected mode, we do nothing but load fs register with the segment selector 16 129 00:10:03,780 --> 00:10:07,820 which specifies the third descriptor for data segment. 130 00:10:08,760 --> 00:10:12,780 And the limit of the data segment here is 4g. 131 00:10:19,800 --> 00:10:26,940 Then we go back to real mode by clearing the pe bit in cr0. At this point, 132 00:10:26,990 --> 00:10:30,330 we should see fs segment register has 4gb segment limits. 133 00:10:31,750 --> 00:10:33,520 We add a break point here. 134 00:10:41,250 --> 00:10:42,210 And see the result. 135 00:10:47,960 --> 00:10:48,800 run the debugger. 136 00:10:55,950 --> 00:10:59,700 We type sreg to check the segment registers. 137 00:11:00,820 --> 00:11:07,270 OK, you can see right now the limits of fs register is 4g, whereas other registers 138 00:11:07,270 --> 00:11:08,590 still have 1mb limits 139 00:11:08,610 --> 00:11:08,980 . 140 00:11:12,870 --> 00:11:17,970 The next thing we are going to do is we are going to load the image. 141 00:11:17,970 --> 00:11:23,620 To load the image, we read a couple of sectors in the low memory address and then copy the data to the high memory address. 142 00:11:24,150 --> 00:11:26,110 This is because the destination address 143 00:11:26,110 --> 00:11:29,120 the bios service uses is 16-bit address. 144 00:11:29,280 --> 00:11:33,330 So we cannot load the image directly to the memory above 1mb. 145 00:11:34,170 --> 00:11:37,820 The code of loading sectors is no different from what we have here. 146 00:11:38,520 --> 00:11:45,510 We just wrap it as a function called read sectors. In the function, we pass the lba value of sectors 147 00:11:45,510 --> 00:11:46,290 we want to read 148 00:11:46,650 --> 00:11:51,180 in register ebx and the sectors count in register ax. 149 00:11:51,930 --> 00:11:56,420 The address we want to load the sectors into is set to 60000. 150 00:11:56,550 --> 00:12:02,220 So the segment part of the address is 6000. Before we return from the function, 151 00:12:03,220 --> 00:12:08,020 if the carry flag is set, we set al to 1, otherwise al is cleared. 152 00:12:09,090 --> 00:12:13,290 So we can check the status of read sectors by testing al register. 153 00:12:14,130 --> 00:12:17,820 Ok we initialize the arguments and call function read sectors. 154 00:12:18,210 --> 00:12:25,020 Remember we have created the image with the chs value being 203 16 63. 155 00:12:25,680 --> 00:12:27,990 This is the total sectors we have in the image. 156 00:12:28,680 --> 00:12:31,670 In this example, we want to read 100 sectors each time, 157 00:12:31,860 --> 00:12:38,040 so we divide it by 100 and move the result to cx. Here cx registers acts as a counter, 158 00:12:38,430 --> 00:12:42,120 we will repeat the process according to cx. 159 00:12:43,140 --> 00:12:50,900 Ebx specifies the sector we want to read, which starts from the first sector. So we clear ebx. 160 00:12:50,910 --> 00:12:57,210 Edi holds the high memory address into which we want to load the file data. Here we set it to this value 161 00:12:57,690 --> 00:12:58,860 which means the memory above it 162 00:12:58,860 --> 00:13:04,890 is used for file system only. And the memory below it is used for the system. 163 00:13:05,730 --> 00:13:14,110 Then we save ecx ebx and edi on the stack because we will use them in the loop. And then initialize ax with 100 164 00:13:14,110 --> 00:13:19,440 and call read sectors. After we return, we test al register, 165 00:13:19,830 --> 00:13:24,030 if al is nonzero, we know that we have an error and jump to read error. 166 00:13:25,040 --> 00:13:29,500 If the operation succeeds, we restore edi and ebx values. 167 00:13:30,640 --> 00:13:35,260 The next thing we are going to do is we are going to move the data to the high memory part. 168 00:13:36,570 --> 00:13:41,500 Function read sectors read 100 sectors and we copy 4 bytes of data each time, 169 00:13:41,850 --> 00:13:47,010 so here we divide the size of 100 sectors by 4. 170 00:13:47,020 --> 00:13:51,660 Read sectors load the data in memory 60000, we save the address to esi. 171 00:13:52,410 --> 00:13:54,060 Ok we start copying the data. 172 00:13:55,240 --> 00:14:01,300 we copy the data into eax. You can see, we specify the segment register fs to address the memory. 173 00:14:01,780 --> 00:14:03,600 If we use other segment registers, 174 00:14:03,760 --> 00:14:05,440 the address is out of limits. 175 00:14:06,680 --> 00:14:11,720 Ok next we move eax to the high memory address specified in register edi. 176 00:14:13,170 --> 00:14:15,640 Right, we have copied 4 bytes of data. 177 00:14:15,780 --> 00:14:16,560 Let's continue. 178 00:14:17,510 --> 00:14:24,320 we increment esi and edi by 4 bytes which move to the next position. And repeat the process 179 00:14:24,680 --> 00:14:25,600 using instruction loop. 180 00:14:26,630 --> 00:14:30,290 After we exit out the loop, 100 sectors of data is copied 181 00:14:31,330 --> 00:14:34,360 and we continue reading remaining sectors until we are done. 182 00:14:36,410 --> 00:14:40,220 So we restore the old ecx value by popping it from the stack. 183 00:14:41,100 --> 00:14:46,290 And don’t forget to adjust ebx by adding ebx 100 and continue the process. 184 00:14:47,780 --> 00:14:53,990 When it is done, we could have remaining sectors smaller than 100 to read into memory. 185 00:14:53,990 --> 00:14:59,860 We read remaining sectors. To get the count of remaining sectors, we mod the total count by 100 186 00:15:00,200 --> 00:15:01,700 and call reading sectors. 187 00:15:02,610 --> 00:15:05,780 After we read the sectors successfully, we can move the data. 188 00:15:07,270 --> 00:15:12,070 Divide the size of the remaining sectors by 4 and save the address of the data in esi 189 00:15:12,130 --> 00:15:13,000 . 190 00:15:14,260 --> 00:15:19,570 The copy part is the same as read fat part. We copy 4 bytes of data each time. 191 00:15:20,690 --> 00:15:27,020 When we are done, all the data of the image is copied in the high memory part. At this point, 192 00:15:27,020 --> 00:15:29,090 we can switch to protected mode and long mode. 193 00:15:31,040 --> 00:15:37,640 In the beginning of this video, I said that the loader has assembly module and c module. In the c module, we will parse the file system 194 00:15:37,640 --> 00:15:43,400 and load our kernel. Therefore, right after we enter 64-bit mode, 195 00:15:43,400 --> 00:15:46,790 we will copy the c module of the loader to the address 196 00:15:47,270 --> 00:15:49,010 100000 and jump there. 197 00:15:50,170 --> 00:15:56,570 So the destination is 100000 in this case. Since we load 15 sectors of data in the boot code, 198 00:15:57,160 --> 00:16:00,280 the size of c module is simply set to 15 sectors. 199 00:16:01,730 --> 00:16:07,700 The virtual address of the destination is also changed to the correct address. In this example, 200 00:16:07,730 --> 00:16:10,130 we will append the c module to the loader.bin. 201 00:16:11,130 --> 00:16:17,130 To reference the base address of the module, we define label c module at the end of the file. 202 00:16:18,260 --> 00:16:20,570 So the c module will be appended here. 203 00:16:22,870 --> 00:16:27,640 It’s worth mentioning that we could have unexpected result when we load the fat16 image. 204 00:16:28,890 --> 00:16:36,620 When we load the fat16 image using the bios disk service, the bios disk service seems to use the fs segment register. 205 00:16:37,320 --> 00:16:42,870 Therefore, we better to initialize the fs segment register after we set the fs limits to 4g. 206 00:16:44,590 --> 00:16:47,410 So here, before we call disk service. 207 00:16:48,380 --> 00:16:57,100 we initialize fs to 0. So we zero out ax register and then mov fs ax 208 00:16:59,740 --> 00:17:05,319 Next we store fs value on the stack before we call disk service, so we push fs 209 00:17:07,170 --> 00:17:10,020 Then we restore it from the stack right after it returns. 210 00:17:11,030 --> 00:17:13,640 So here we pop fs. 211 00:17:15,660 --> 00:17:17,339 Another location we need to fix 212 00:17:18,680 --> 00:17:20,540 Is the read remaining sectors here 213 00:17:21,599 --> 00:17:24,000 So we also need push fs 214 00:17:25,540 --> 00:17:27,500 and then pop fs after we 215 00:17:28,700 --> 00:17:30,200 return from read sectors. 216 00:17:31,460 --> 00:17:32,800 Alright, this file is done. 217 00:17:34,410 --> 00:17:41,190 let’s discuss what we will have in the c module. The tasks we will do include file system initialization, 218 00:17:41,190 --> 00:17:44,740 load kernel and user program. 219 00:17:45,660 --> 00:17:47,250 While we are doing the tasks, 220 00:17:47,250 --> 00:17:50,580 we also print string and using assertion to do the check. 221 00:17:51,420 --> 00:17:55,170 So, we need print module, debug module, library file and entry file. 222 00:17:55,240 --> 00:18:02,370 As you see, I have added them. These files are the same files we created in the kernel. 223 00:18:02,460 --> 00:18:05,190 So we just copied them from the kernel project. 224 00:18:06,000 --> 00:18:11,640 The entry file is pretty much the same as kernel assembly file which is used to prepare for the main function 225 00:18:11,640 --> 00:18:12,100 . 226 00:18:12,930 --> 00:18:13,950 So let's take a look. 227 00:18:17,070 --> 00:18:21,200 The only thing we will do is set stack pointer and jump to main function. 228 00:18:22,510 --> 00:18:25,000 OK, let's move to main.c file. 229 00:18:28,130 --> 00:18:34,520 This file is simple, in the main function we call printk function to print the message loader is working. 230 00:18:35,840 --> 00:18:38,810 The last file we will talk about is linker script. 231 00:18:42,080 --> 00:18:45,090 remember in the loader file we jump to this virtual address. 232 00:18:45,830 --> 00:18:47,280 This is the base address 233 00:18:47,300 --> 00:18:49,300 where we load the c module of the loader file, 234 00:18:50,920 --> 00:18:58,060 After we set base address in the linker script, we can append the c module to the loader.bin file in the build script. 235 00:18:59,520 --> 00:19:02,220 The build script is only for the loader project. 236 00:19:04,380 --> 00:19:09,120 You can see it compiles the c module and generate entry.bin file. 237 00:19:10,330 --> 00:19:16,120 Instead of writing the entry file to the image, we append it to the loader.bin by specifying the entry file 238 00:19:16,120 --> 00:19:22,960 and pointing to loader.bin. And then we write the loader.bin file to the image. 239 00:19:24,290 --> 00:19:27,500 Here we set the size of the loader to 15 sectors. 240 00:19:28,860 --> 00:19:31,820 Alright, that's it. Let's build the project and test it out. 241 00:19:37,100 --> 00:19:38,180 We run bochs. 242 00:19:42,920 --> 00:19:44,840 OK, you can see loader is working. 243 00:19:46,130 --> 00:19:47,570 That's it for this lecture. 244 00:19:47,600 --> 00:19:48,950 See you in the next video.