1 00:00:00,240 --> 00:00:07,050 Let’s continue from where we left off. After long mode check is passed, what we are going to do next is we are going to 2 00:00:07,050 --> 00:00:10,020 load our kernel file. 3 00:00:10,020 --> 00:00:11,790 As we have loaded the loader file, loading kernel 4 00:00:11,790 --> 00:00:16,260 is pretty much the same except that there are several parameters need to change. 5 00:00:17,560 --> 00:00:19,810 So we just copy and paste the code here. 6 00:00:22,050 --> 00:00:24,870 We create a label called load kernel 7 00:00:32,950 --> 00:00:38,440 First off, we take a look at memory map and decide where we load the kernel file into memory. 8 00:00:39,970 --> 00:00:46,800 As you can see, this is a rough demo of the memory usage in the boot process. At the bottom we have the interrupt vectors 9 00:00:46,810 --> 00:00:48,390 and bios data. 10 00:00:49,240 --> 00:00:56,790 Then we get a small area of free memory. At address 7c00 our boot code is loaded by the BIOS. 11 00:00:58,090 --> 00:01:05,140 And the boot code load the loader file at address 7e00. Then we get 12 00:01:05,140 --> 00:01:11,060 a relatively large area of free memory we can use. Our kernel can be loaded here. 13 00:01:11,080 --> 00:01:18,120 The area above it is reserved for hardware and bios data. Starting from 100000 which is 1mb, 14 00:01:18,400 --> 00:01:19,560 we have free memory again. 15 00:01:19,590 --> 00:01:26,470 Note that this area also gets some space reserved for hardware which is not available for us. 16 00:01:27,700 --> 00:01:34,300 And the memory map may vary among different computers. Therefore, we need to get a memory map 17 00:01:34,300 --> 00:01:38,790 by calling the bios service so that we know which part of the memory is available to use. 18 00:01:40,650 --> 00:01:42,840 We will see how to do it in the next lecture. 19 00:01:44,290 --> 00:01:49,450 So with this in mind, we choose to load the kernel at address 10000 20 00:01:50,530 --> 00:01:56,200 and when we jump to 64-bit mode, we copy the kernel into the memory above 1mb. 21 00:01:56,890 --> 00:01:58,360 Alright, back to our file. 22 00:02:00,610 --> 00:02:06,670 here we want to load 100 sectors of data roughly 50 kilobytes which is enough for our kernel. 23 00:02:10,620 --> 00:02:15,000 The memory address we choose to load the kernel at is 10000. 24 00:02:16,220 --> 00:02:19,520 Note that we cannot assign 10000 25 00:02:21,690 --> 00:02:22,650 to a word variable. 26 00:02:23,790 --> 00:02:28,730 10000 in hexadecimal is larger than what a word or two bytes can represent. 27 00:02:29,570 --> 00:02:32,430 We will get an overflow and result is not correct if we do it 28 00:02:32,460 --> 00:02:33,160 . 29 00:02:33,790 --> 00:02:35,130 Instead, we assign 30 00:02:38,260 --> 00:02:41,180 1000 to segment part of the address 31 00:02:43,060 --> 00:02:51,670 and leave offset 0. The physical address we get is 1000*16+0 equals 10000. 32 00:02:53,580 --> 00:02:56,060 The boot file resides in the first sector. 33 00:02:56,490 --> 00:02:59,250 The loader file occupies the next five sectors. 34 00:03:00,450 --> 00:03:03,330 So we write our kernel from the seventh sector. 35 00:03:04,580 --> 00:03:05,630 We write 6, 36 00:03:07,970 --> 00:03:10,490 remember sector value is zero-based value. 37 00:03:11,940 --> 00:03:17,760 Don’t forget to check carry flag. If carry flag is set after calling the read service, 38 00:03:17,790 --> 00:03:22,500 we know that loading kernel failed, and we jump to read error and stop here. 39 00:03:25,530 --> 00:03:28,920 we place the label read error at the end of the code. 40 00:03:35,330 --> 00:03:39,290 We haven't defined the read packet, so we define the read packet here. 41 00:03:45,050 --> 00:03:48,800 Remember the package is a structure which is 16 bytes 42 00:03:55,530 --> 00:03:58,830 If everything goes as expected, we just print the message 43 00:04:00,560 --> 00:04:01,550 kernel is loaded. 44 00:04:04,420 --> 00:04:09,130 OK, we save the file and build the project in the terminal. 45 00:04:11,920 --> 00:04:19,810 OK, the binary file is written into the boot image and also it's worth mentioning that we don't actually 46 00:04:19,810 --> 00:04:25,040 load the kernel file into memory because we didn't create the kernel file at this point. 47 00:04:25,780 --> 00:04:31,660 So the code in this example just reads whatever is in the image and load it into memory. 48 00:04:32,350 --> 00:04:34,630 In the next section, we will load the real kernel. 49 00:04:35,640 --> 00:04:38,640 So right now we only print the message on the screen.