1 00:00:00,560 --> 00:00:06,440 After disk extension check is passed, we load a new file called loader in the memory. 2 00:00:07,400 --> 00:00:14,360 The reason we need a loader file is that the MBR is fixed size which is 512 bytes. 3 00:00:14,700 --> 00:00:18,960 There are spaces reserved for other use such as partition entries 4 00:00:18,960 --> 00:00:23,640 entries which leaves us less than 512 bytes for the boot code. 5 00:00:24,300 --> 00:00:29,600 The tasks that we should do in the boot process includes load kernel file, 6 00:00:29,900 --> 00:00:34,760 get memory map, switch to protected mode and then jump to long mode. 7 00:00:34,920 --> 00:00:39,880 Doing all these tasks requires the boot code larger than 512 bytes. 8 00:00:40,510 --> 00:00:45,410 So here we introduce a new file loader file to do all these things. 9 00:00:45,680 --> 00:00:49,480 The loader file has no 512 bytes limits. 10 00:00:50,640 --> 00:00:54,160 This is the memory map when we load the loader file. 11 00:00:54,600 --> 00:00:59,640 The boot code is loaded by BIOS in the memory address 7c00. 12 00:00:59,840 --> 00:01:05,680 The size of the boot code is 512 bytes which is 200 in hexadecimal. 13 00:01:05,760 --> 00:01:10,800 So here we simply load the loader file into the location right after the boot code 14 00:01:10,800 --> 00:01:13,960 which is at the location 7e00. 15 00:01:14,600 --> 00:01:16,920 Ok back to our project. 16 00:01:18,360 --> 00:01:23,960 As we did before, we define a label load loader. 17 00:01:28,320 --> 00:01:33,960 The disk service we use is interrupt 13. 18 00:01:35,920 --> 00:01:38,680 The function code is 19 00:01:38,680 --> 00:01:42,640 42 saved in ah 20 00:01:44,360 --> 00:01:48,520 which means we want to use disk extension service. 21 00:01:48,520 --> 00:01:55,440 And also, don’t forget to save the drive id to dl register before we call the service. 22 00:02:03,520 --> 00:02:08,680 Notice that the drive id has been saved in value drive id. 23 00:02:09,400 --> 00:02:16,320 The parameter we pass to the service is actually a structure. Let's define the strcuture, 24 00:02:17,920 --> 00:02:20,120 we call it read packet 25 00:02:24,560 --> 00:02:28,680 the size of the structure is 16 bytes. 26 00:02:33,000 --> 00:02:39,880 we mov the address of read packet to si register 27 00:02:40,840 --> 00:02:44,840 Now si holds the address of read packet. 28 00:02:46,520 --> 00:02:51,680 The first word holds the value of structure length. 29 00:02:55,520 --> 00:02:59,040 So we move 16 30 00:02:59,600 --> 00:03:05,520 or 10 in hexadecimal to it, because readpacket is 16 bytes. 31 00:03:06,080 --> 00:03:10,680 The second word is the number of sectors we want to read. 32 00:03:14,480 --> 00:03:22,360 Since the loader in this course is a small file, we simply read 5 sectors which is enough space for the loader. 33 00:03:24,640 --> 00:03:30,640 The next two words specify the memory location into which we want to read our file. 34 00:03:31,280 --> 00:03:37,000 As we have talk about, we load the loader file into the memory address 7e00. 35 00:03:37,720 --> 00:03:43,960 So we save 7e00 in the first word 36 00:03:48,920 --> 00:03:55,400 which is the offset. The second word holds the value of segment part of the address. 37 00:03:55,880 --> 00:03:58,160 Here we simply set it to 0. 38 00:04:02,440 --> 00:04:10,800 So the logical address is 0 7e00 and the physical address it points to is 7e00. 39 00:04:12,160 --> 00:04:16,480 The last two words are the 64-bit logical block address. 40 00:04:16,480 --> 00:04:22,920 The loader file will be written into the second sector of the disk. Therefore, we use lba 1. 41 00:04:24,080 --> 00:04:33,600 Remember logical block address is zero-based address. Meaning that the first sector is sector 0, the second sector is sector 1 and so on. 42 00:04:35,040 --> 00:04:42,520 So we save 1 to the lower half of the 64-bit address. 43 00:04:46,840 --> 00:04:49,440 The higher part is simply set to 0. 44 00:04:54,200 --> 00:04:58,640 Now with all the parameters prepared, we call interrupt 13. 45 00:04:58,640 --> 00:05:05,320 If it fails to read sectors, the carry flag is set. So we use jc instruction 46 00:05:07,000 --> 00:05:10,680 and jump to label read error. 47 00:05:13,400 --> 00:05:20,880 What we are going to do next is change the print message part so that it prints error message on the screen. 48 00:05:20,880 --> 00:05:24,000 Remove the label print message 49 00:05:26,160 --> 00:05:32,720 and place read error and not support. 50 00:05:40,400 --> 00:05:48,320 If disk extension service is not supported or read error occurs, we print read error in boot process. 51 00:05:48,320 --> 00:05:51,600 we change the message to 52 00:05:52,680 --> 00:05:57,520 we have an error in boot process. 53 00:06:00,560 --> 00:06:04,040 Ok we have finished handling the error case. 54 00:06:04,040 --> 00:06:11,960 When we successfully read the loader file into memory, the next thing we are going to do is we are going to jmp to the start of loader 55 00:06:12,760 --> 00:06:21,000 and don’t forget to pass the drive id to the loader, because we need to loader kernel file using drive id. 56 00:06:24,400 --> 00:06:29,080 We simply pass drive id to dl register. 57 00:06:29,480 --> 00:06:34,800 and jump to 0x7e00 58 00:06:35,240 --> 00:06:40,360 which is the address of memory into which we load our loader from disk. 59 00:06:40,360 --> 00:06:45,960 Next thing we are going to do is write loader file. We create a new assembly file. 60 00:06:49,440 --> 00:06:51,240 called loader file. 61 00:06:59,040 --> 00:07:05,000 Just like the boot file, we specify the code is running in 16-bit mode 62 00:07:06,120 --> 00:07:15,280 and the loader is supposed to be running at address 7e00. 63 00:07:16,520 --> 00:07:19,760 Then we define the label start 64 00:07:21,160 --> 00:07:31,440 indicating that this is the start of the loader. As for the end part, we use infinite loop to halt the processor as we did in the boot file. 65 00:07:35,040 --> 00:07:46,760 In this lecture, we only demonstrate how to load loader file. So we just print message on the screen. The print function is the same as the one in boot file. So we copy and paste it here 66 00:07:52,320 --> 00:07:57,560 the message we want to print is loader starts for example . 67 00:07:59,560 --> 00:08:03,720 When the loader is running, it should print loader starts.Now let’s build this project. 68 00:08:03,720 --> 00:08:07,960 Now let’s build this project. We save these two files. 69 00:08:10,360 --> 00:08:14,680 Since we have loader file, we add the file in the build script. 70 00:08:27,360 --> 00:08:30,760 The output file is also binary file 71 00:08:30,760 --> 00:08:35,600 the name of the file is called loader.bin. 72 00:08:39,840 --> 00:08:46,160 Once we generate the loader file, we need to write it into boot image 73 00:08:46,160 --> 00:08:53,440 When we load the loader file using bios serivce, we read 5 sectors of data in memory. 74 00:08:58,080 --> 00:09:01,800 So here we sepcify count to 5 75 00:09:02,760 --> 00:09:06,880 to write 5 sectors of data in boot image. 76 00:09:06,880 --> 00:09:13,480 The first sector of boot image stores boot.bin, and we write loader from the second sector. 77 00:09:13,480 --> 00:09:15,480 So here we use seek 78 00:09:15,480 --> 00:09:20,120 which means skip blocks at the beginning of the output. 79 00:09:20,120 --> 00:09:23,960 So 1 means we skip the first block. 80 00:09:24,800 --> 00:09:27,880 ok let's open the terminal, 81 00:09:33,240 --> 00:09:34,880 and run the script. 82 00:09:38,240 --> 00:09:44,400 The message shows that the two files have been written into the boot image. 83 00:09:44,400 --> 00:09:50,000 With the boot image is ready to load, let's test it in virtual machine first. 84 00:09:56,720 --> 00:09:59,680 As you see, Loader starts is printed. 85 00:10:01,280 --> 00:10:05,000 Here shows the message loader starts on the real machine. 86 00:10:05,840 --> 00:10:09,680 That's it for this lecture, see you in the next video.