Multi-guide for Linux Programming

Source: Internet
Author: User
Linux programming implementation multi-boot-general Linux technology-Linux programming and kernel information. The following is a detailed description. If you are a computer user, believe that you have installed the operating system. If you have been using an operating system for a long time, you may install a new operating system, but you often want to keep the current operating system. For example, if you have a Windows 98, you can install Windows 2 K or Windows XP on it. This is because NTloader in Windows can recognize operating systems earlier than itself, therefore, you can retain the previous operating system. But if you already have a Windows 2 K or Windows XP, you need to install another Windows 98 on the hard disk, or even some people with nostalgia want to install a DOS6.22 on it. What should you do now? You often use third-party software, such as V communications,. inc's System Commander series, and PowerQuest javasration BootMagic. These software sets hard disk partitions as two or more primary partitions (that is, the dual "C" disk, or multiple "C" disks ), install different operating systems into different primary partitions and activate different partitions for multi-boot. The advantage of this is security and stability. Once an operating system crashes, this does not affect other operating systems. I personally think it is recommended.
This article is not an introduction to the implementation of multiple operating systems, so I will not waste any space to introduce the use of third-party software such as BootMagic, I want to analyze the implementation principles of such software.
To understand its implementation principle, you must first have a certain understanding of the overall structure of the hard disk. If you do not know its structure, refer to table 1.
Table 1:
Name remarks
1. The Master boot sector occupies one sector.
2. The Boot sector of the first primary partition occupies one sector.
3. the space occupied by the FAT area of the first primary partition is determined by the disk size and FAT type.
4. the root directory of the first partition stores the root directory of the partition.
5. The first partition data area is used to store various types of data.
6...
7. The extended partition table occupies one slice
8. The pilot sector of the first extended partition occupies one sector.
9 the space occupied by the FAT area of the first extended partition is determined by the disk size and FAT type.
10. the root directory of the first extended partition stores the root directory of the partition.
11 The first data area in the extended partition is used to store various types of data.
12...
Note: This table is described in spatial order. A hard disk can contain up to four primary partitions and multiple extended partitions.

We also need to understand the structure of the Master boot sector. It is the first sector of the hard disk, and the Master boot Record (MBR ), disk Partition Table (DPT) is composed of three parts. Its structure is shown in table 2.



Table 2:
Position name remarks in the primary Boot Sector
1 offset 00H ~ The 1BDH main boot program occupies 446 bytes, but the actual usage may not be full.
2 offset 1BEH ~ The primary partition table of a 1 FDH hard drive occupies 64 bytes, and each partition item occupies 16 bytes. A maximum of four partition items are allowed: 1BEH ~ 1CDH partition item 11CEH ~ 1DDH partition item 21DBH ~ 1EDH partition item 31EFH ~ 1FDH partition Item 4
3 offset 1FEH ~ The 1FFH end ID occupies 2 bytes to determine the validity of the partition table.

I will not analyze the main boot program here. Note that there are some error messages in the Main Boot Program, some Boot viruses often overwrite this information to embed themselves into the main boot program, and change the first few bytes of the Main Boot Program to a jump command, by observing the changes in the main boot program, we can often see whether our computer has a boot virus. Once an exception is found, we can use the FDISK/MBR command to overwrite the original MBR. The two bytes of the end identifier are always 55 h aah. This identifier is only used to determine whether this sector is a valid primary boot sector. If the primary boot program finds that this end identifier does not exist, the System is deemed to be Missing and "Missing Operating System" is displayed ".
The last thing we need to know is the hard disk partition table. The multi-boot Software generally aims to achieve multi-boot by modifying the specific bytes in the partition table. In the master boot area of the hard disk, the offset from 1 beh ~ The 64 bytes of 1FDH are stored in the hard disk partition table, which contains a maximum of 4 partition items. Each partition item consists of 16 bytes. Let's take a look at the detailed meaning of each partition item, assume that the 16 bytes occupied by a partition are marked as 00H ~ 0FH. Its structure is shown in table 3:
Table 3:
Offset description
The value of the H pilot Identifier byte can only be H or 80 H. If it is H, it indicates that the partition is not bootable. If it is 80 H, it indicates that the partition can be used for boot.
Start head number of the 01H Partition
2 h, the height is the high position of the Start cylindrical Number of the partition, and the bottom 6 is the start sector number of the partition.
Low Position of the Start cylindrical number in the 03H Partition
04H partition system flag. If this value is H, it indicates that this partition is an unrecognized system. If it is 04H, this partition is FAT16; if it is 05H or 0FH, this partition is an extended partition; if the value is 0 B, the partition is FAT32. If the value is 83H, the partition is Linux. If the value is 07, the partition is NTFS.
05H partition end head number
06H its height is the high position of the ending cylindrical Number of the partition, and the bottom 6 is the ending fan area number of the partition.
The low position of the ending cylindrical number in the H Partition
08H ~ The Slice address of the 0BH partition in linear addressing mode.
0CH ~ 0FH the partition size in units of sectors (total number of sectors)
Let's take a look at how software such as BootMagic implements multi-system guidance, in table 3, we can see that a byte at the offset of H is a boot zone in the table when its value is 80 h. When its value is H, the partition is not a boot zone, in fact, the implementation principle of multi-pilot is to change the value of this field to select the partition to be guided.
If you have understood the above principles, we can make multiple boot programs of our own. Here I will write a complete implementation program, I will analyze how this program should be written. Suppose you have a hard disk and you have installed Windows XP on it, but now you want to install Windows 98, so you use PartitionMagic to set your first extended partition, that is, your d partition as the primary partition, activate it, restart it, and install the CD boot system in Windows 98, and install Windows 98. So far, don't rush to install BootMagic first. Let's try your own BootMagic.
Obviously, to implement this function, at least two modules are required. Module 1 is written into the primary Boot Sector to replace the original MBR zone. It must be able to obtain the execution right before system guidance, after the Pilot Selection of the primary partition is realized, the execution right is handed over to the selected operating system for multi-system guidance. Module 2: three functions are implemented, first, back up the original primary boot sector to another sector (in my example, back up it to 0 heads, 0 channels, and 6 fans), and then write module 1 to the primary Boot Sector. In addition, Module 2 implements Multi-Pilot Program uninstallation.
The function of Module 2 is relatively simple, and its source code is as follows:
; Hardware Multi-boot Installer (SYSGUIDE. EXE) Module 2
DATA SEGMENT
D1 DB 'choose'
D2 DB 'Please choose the number (1) Install the sysguide', 0dh, 0ah, 'db' (2) Delete the sysguide', 0dh, '$'
D3 DB 'sysguide. DAT ', 00 H
D4 DB 'can not find file (SYSGUIDE. DAT) ', 0dh, 0ah,' $'

D5 DB 0dh, 0ah
DB 0dh, 0ah
DB 0dh, 0ah
DB '---------- # much system guide # ------------', 0dh, 0ah
DB 'e-mail: tyhhyf@hotmail.com ', 0dh, 0ah
DB 'oss: 100', 0dh, 0ah, '$'
DATA ENDS


CODE SEGMENT
Assume cs: CODE, DSATA, ESATA
START:
Mov ax, DATA
Mov ds, AX
Mov es, AX
Mov dx, OFFSET D5
Mov ah, 09 H
INT 21 H
Mov ah, 00 H
INT 16 H
Mov dx, OFFSET D2
Mov ah, 09 H
INT 21 H
Readkey:
Mov ah, 00 H
INT 16 H
Cmp al, '1'
Je key1
Cmp al, '2'
Je key2
Jmp readkey
Key1:
Mov ax, 0201 H; read 0 0 1 Sector
Mov cx, 0001 H
Mov dx, 0080 H
Mov bx, 0F000H
INT 13 H
MOVSI, offset D1
MOVDI, 0F020H
MOVCX, 0003 H
REPZ CMPSW
JZEXIT; jump if the string is the same
Mov ax, 0301 h; back up the original MBS area to 0 0 6
Mov cx, 0006 h
Mov dx, 0080 h
Int 13 h
Mov ah, 3DH
Mov dx, OFFSET D3
Mov al, 00 H
INT 21 H; open the file SYSGUIDE. DAT
JB OPERERROR
PUSH AX
Mov ah, 3FH
Mov dx, 0F000H
Mov cx, 018DH
POP BX
INT 21 H
Mov ax, 0301 H; write SYSGUIDE. DAT to 0 0 1 fan, replace the original MBR
Mov cx, 0001 H
Mov dx, 0080 H
Mov bx, 0F000H
INT 13 H
JMP EXIT
Key2:
Mov ax, 0201 H; read 0 0 1 Sector
Mov cx, 0001 H
Mov dx, 0080 H
Mov bx, 0F000H
INT 13 H
MOVSI, offset D1
MOVDI, 0F020H
MOVCX, 0003 H
REPZ CMPSW
JNZEXIT; jump if the string is the same
Mov ax, 0201 H; read 0 0 6 sectors
Mov cx, 0006 H
Mov dx, 0080 H
Mov bx, 0F000H
INT 13 H
Mov ax, 0301 H; write 0 0 1 Sector
Mov cx, 0001 H
Mov dx, 0080 H
Mov bx, 0F000H
INT 13 H
JMP EXIT
OPERERROR:
Mov ah, 09 H
Mov dx, OFFSET D4
INT 21 H
EXIT:
Mov ah, 4Ch
INT 21 h
CODE ENDS
END START
Module 1 is written to the first sector of the hard disk, and the execution right is obtained before the system boot, so as to implement multi-boot. This is a sector program. Different from general program files, the format is similar to that of COM files. Here I use the DEBUG program to extract methods from the EXE file. The source program is as follows:
Multi-Pilot Program (SYSGUIDE. DAT), which needs to be extracted from the program files generated by the following source program (Module 1)
CODE SEGMENT
Assume cs: CODE, DS: CODE, ES: CODE

This section moves the entire hard disk lock from 0000: 7c00 to 0000: 0600 to avoid being overwritten by the code later read.
START:
Db 256 dup (00 h)
PUSHDS
PUSHES
MOVCX, 0100 h
Movable Di, 0600 h
Movax, 0000 h
MOVDS, AX
MOVES, AX
MOVSI, 7C00h
REPNZ MOVSW
Mov ax, 0686 h
JMPax; Code after long jump to move, that is, run from screen
Words db 'Please choose the partition for guide .'
Db 10 dup (20 h)
Db' (1) partition 1; (2) partition 2; (3) partition 3; (4) partition 4; ', 00 h
Screen:
Movax, 0600 h
MOVBH, 0F0h
MOVCX, 0000 h
MOVDX, 184Fh
INT10h; initialize the screen (the foreground is black, the background is gray, and the characters are flashing)
MOVDL, 1Ah
MOVSI, 0619 h; display characters from 0000 h (because the program will be read at 0600,
; Actually, the string starting from the current words)
DISPLAY:
Mov ah, 02 H
Mov dh, 10 H
Mov bh, 00 H
INT 10 H
Mov al, [SI]
Cmp al, 00 H
JE readMBS
Mov ah, 09 H
Mov cx, 01 H
Mov bh, 00 H
Mov bl, 70 H
INT 10 H
INC DL
INC SI
JMP DISPLAY
ReadMBS:
Movax, 0201 h
MOVCX, 0001 h
MOVDX, 0080 h
MOVBX, 1000 h
INT13h; read the Master Boot area (SYSGUIDE. DAT) to 0000: H
MOV ax, 0201 h
MOV cx, 0006 h; read 0 0 6 fans (that is, the original MBS)
MOV dx, 0080 h
MOV bX, 07C00h
Int 013 h
Readkey:
Mov ah, 00 H
INT 16 H
Cmp al, '1'; is it 1?
JE number1
Cmp al, '2'
JE number2
Cmp al, '3'
JE number3
Cmp al, '4'
JE number4
Jmp readkey
Number1:
Db 0a0h, 0c2h, 11 h; equivalent to mov al, [011c2h], because mov al, [011c2h] cannot be compiled
Cmp al, 00 h
Je error1
Cmp al, 05 h
Je error1
Cmp al, 0fh
Je error1
Mov ah, 080 h
Db 88 h, 26 h, 0beh, 11 h; equivalent to mov [011beh], ah
Mov ah, 00 h
Db 88 h, 26 h, 0ceh, 11 h; mov [11ceh], ah
Db 88 h, 26 h, 0deh, 11 h; mov [11deh], ah
Db 88 h, 26 h, 0eeh, 11 h; mov [11eeh], ah
Jmp OK
Number2:
Db 0a0h, 0d2h, 11 h; mov al, [11d2h]
Cmp al, 00 h
Je error1
Cmp al, 05 h
Je error1
Cmp al, 0fh
Je error1
Mov ah, 80 h
Db 88 h, 26 h, 0ceh, 11 h; mov [11ceh], ah
Mov ah, 00 h
Db 88 h, 26 h, 0beh, 11 h; mov [11beh], ah
Db 88 h, 26 h, 0deh, 11 h; mov [11deh], ah
Db 88 h, 26 h, 0eeh, 11 h; mov [11eeh], ah
Jmp OK

Error1:
Jmp readkey

Number3:
Db 0a0h, 0e2h, 11 h; mov al, [11e2h]
Cmp al, 00 h
Je error1
Cmp al, 05 h
Je error1
Cmp al, 0fh
Je error1
Mov ah, 80 h
Db 88 h, 26 h, 0deh, 11 h; mov [11deh], ah
Mov ah, 00 h
Db 88 h, 26 h, 0beh, 11 h; mov [11beh], ah
Db 88 h, 26 h, 0ceh, 11 h; mov [11ceh], ah
Db 88 h, 26 h, 0eeh, 11 h; mov [11eeh], ah
Jmp OK
Number4:
Db 0a0h, 0f2h, 11 h; mov al, [11f2h]
Cmp al, 00 h
Je error1
Cmp al, 05 h
Je error1
Cmp al, 0fh
Je error1
Mov ah, 80 h
Db 88 h, 26 h, 0eeh, 11 h; mov [11eeh], ah
Mov ah, 00 h
Db 88 h, 26 h, 0ceh, 11 h; mov [11ceh], ah
Db 88 h, 26 h, 0deh, 11 h; mov [11deh], ah
Db 88 h, 26 h, 0beh, 11 h; mov [11beh], ah
Jmp OK
OK:
Mov ax, 0301 h
Mov cx, 0001 h
Mov dx, 0080 h
Mov bx, 1000 h
Int 13 h; write the partitioned table with the guide label changed to 0 0 1
MOVCX, 040 h
MOVDI, 7 dbeh
MOVSI, 11beh
REPNZ MOVSW
MOV ax, 0301 h
MOV cx, 0006 h; write the partitioned table with the guide label changed to 0 0 6
MOV dx, 0080 h
MOV bX, 07C00h
Int 013 h
MOV bx, 7c00h; place the execution right at 0000: 7C00
JMP bx; long jump to the original MBR code for execution

CODE ENDS
END START
The code is finished now, and the program itself is not complicated. As long as you understand the implementation principle, you will be comfortable with the implementation. Well, let's compile our source code and link it quickly, name the program in Module 2 as SYSGUIDE. EXE, extracts the program we want from the program obtained by Module 1 and name it SYSGUIDE. DAT. The extraction method is as follows:
1. in DOS mode, enter "debug guide. EXE ", where GUIDE. EXE is the program file obtained by the module. EXE program ),
2. Enter r cx (Press ENTER) 1BD (Press ENTER) N (Press ENTER) SYSGUIDE. DAT (Press ENTER) W (Press ENTER) Q (Press ENTER) at the DEBUG prompt ). In this way, you will find a SYSGUIDE in your current directory. the DAT file is the program file used to write the first sector of the hard disk. copy the EXE file to a folder.
Now that the program we want has been obtained, let's use it. Run it in WIN98 we finally installed. When the following prompt is displayed, type the number key "1 ".
'Please choose the number (1) Install the SYSGUIDE
(2) Delete the SYSGUIDE
Then restart the instance. How do you see the boot selection prompt? However, the interface is not very nice. ^ _ ^, But nothing. After all, this is a multi-pilot program we have written! Okay, let's try "1". Is it in the WINXP we installed earlier? Then restart and select "2" to try again. Is it the WIN98 we installed later!
By the way, it seems that another feature of BOOTMAGIC is not implemented, that is, the program automatically identifies the operating system. I think this feature will be extended by our readers! Here I will give a general explanation of the principle and return to table 3. Can you see the byte offset of 04? BOOTMAGIC uses this principle to intelligently identify the operating system. How can this problem be solved? I don't want to talk about it. I want to know it myself.
It's time to say 886 again. It's no longer necessary to write it down and edit it! In this case, if you are interested, contact me to find the method in my source program. Programs can go to http://lsky.net/download/soft.asp? Id = 35 download.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.