获取INT 16h键扫描码而不是字符

问题描述 投票:1回答:1

我正在写一个简单的bootloader,我有一个getch函数。

char getch()
{
   uint16_t inchar;

   __asm__ __volatile__ ("int $0x16\n\t"
                        : "=a"(inchar)
                   : "0"(0x0));

   return (char)inchar;
}

我尝试了第一个明显的解决方案,即删除inchar变量的char的转换,但是当我打印它仍然返回char而不是代码。

我的println实施:

void println(char *str)
{
    while (*str) 
    {
        // AH=0x0e, AL=char to print, BH=page, BL=fg color
        __asm__ __volatile__ ("int $0x10"
                              :
                              : "a" ((0x0e<<8) | *str++),
                                "b" (0x0000));

    }

}

链接脚本:

ENTRY(start);
SECTIONS
{
    . = 0x7C00;
    .text : {
        /* Place the code in hw.o before all other code */
        boot.o(.text);
        *(.text);
    }

    /* Place the data after the code */
    .data : SUBALIGN(4) {
        *(.data);
        *(.rodata);
    }

    /* Place the boot signature at VMA 0x7DFE */
    .sig : AT(0x7DFE) {
        SHORT(0xaa55);
    }

    /* Place the uninitialised data in the area after our bootloader
     * The BIOS only reads the 512 bytes before this into memory */
    . = 0x7E00;
    .bss : SUBALIGN(4) {
        __bss_start = .;
        *(COMMON);
        *(.bss)
        . = ALIGN(4);
        __bss_end = .;
    }
    __bss_sizeb = SIZEOF(.bss);

    /* Remove sections that won't be relevant to us */
    /DISCARD/ : {
        *(.eh_frame);
        *(.comment);
        *(.note.gnu.build-id);
    }
}

我的目的是实现scanf函数,并需要知道Enter键的扫描码。当scanf遇到Enter时,它应该停止读取键盘并返回写入字符串或整数的内容,具体取决于您是编写数字还是字符。

我尝试实施scanf

char* readln() 
{
    char *s[255]; 
    for (int i = 255; i <= 255; ++i) {
        char a[] = {0, 0};
        a[0] = getch();
        s[i] = a[0];
        //println(a);
        if (a[0] == '\r') { 
            break;
            return s;
        }
    } 
} 

当我尝试链接时,它不起作用。它应该返回一个字符串,其中包含从键盘输入的字符。我从链接器收到此错误:

ld:.sig加载在[0000000000007dfe,0000000000007dff]重叠部分.data加载在[0000000000007dd8,0000000000007e15]

gcc assembly x86 bootloader bios
1个回答
5
投票

Int 0x16/AH=0返回返回值的高16位的扫描码。 inchar实际上被定义为16位uint16_t类型。您需要做的就是将inchar的值移到右边8位,将BIOS扫描码放在高8位到低8位。

该功能可能如下所示:

/* getch that returns the scancode and not the ASCII character */
char getch_scancode()
{
   uint16_t inchar;

   /* upper 8 bits of inchar are the scancode in AH. */
   __asm__ __volatile__ ("int $0x16\n\t"
                        : "=a"(inchar)
                        : "0"(0x0));

   /* Shift right 8 bits to move scan code to the lower 8-bits */
   return ((char)(inchar>>8));
}

您不需要扫描码来确定是否按下了ENTER键。您可以从getch测试ASCII字符的值0x0d(回车)。您还可以针对C转义序列\r测试角色。


您获得的链接器错误:

ld:.sig加载在[0000000000007dfe,0000000000007dff]重叠部分.data加载在[0000000000007dd8,0000000000007e15]

是说你的.data部分已开始重叠.sig部分。您使用的链接描述文件是为有限的512字节引导加载程序设计的。发生错误的原因是您现在拥有的代码和数据多于512字节。使用新的链接描述文件和更复杂的技术,您可以让引导加载程序将内核的其余部分读入内存,然后将控制转移到内存中。下面的代码是一个例子:

  • 引导加载程序和内核文件与链接描述文件结合使用。引导签名0xaa55由链接器生成。
  • 将引导加载程序重新定位到中断向量表上方的低内存和位于0x0600的BIOS数据区(BDA)。
  • FAR JMP用于将控制转移到重定位的引导加载程序。
  • 在重新定位的引导加载程序之后,内核将在0x800读入内存。
  • 发生错误时会重试磁盘读取。
  • 使用LBA to CHS translation一次读取1个扇区的内核。这允许它用在软盘映像上。要读取的扇区数由链接器生成。
  • 内核在引导加载程序之后立即存储在扇区中的磁盘上。
  • 该代码假设一张1.44MB的软盘,每个磁道有18个扇区,2个磁头。建议软盘映像包含适当的BIOS Parameter Block (BPB),以便与USB Floppy / FDD仿真兼容。
  • 堆栈设置为0x0000:0x0000。这意味着它将换行到第一个64kb内存的顶部。第一次推送后,堆栈地址将为0x0000:0xfffe。
  • BSS段被清零。我们不能假设记忆将为零。
  • 在调用内核之前,段寄存器设置为零。 CS = DS = ES = FS = GS = 0并且字符串指令的方向标志被清除以进行向前移动。
  • 使用32位(DWORD)偏移量将控制转移到C代码中的kernelmainentry点,以便与使用-m16选项时GCC处理返回地址的方式兼容。
  • 我提供了一些改进和改变的功能。用于打印单个字符的printchar,用于打印NUL终止字符串的printstring,以及开始接受键盘用户输入的getstring
  • getstring获取缓冲区和最大字符数。它在用户按下ENTER时结束。 TAB被忽略并丢弃。 BACKSPACE可以防止退回缓冲区的开头,并将退格区视为破坏性的,因为它会备份光标并用空格替换它。
  • 示例内核会提示输入用户名,并将其显示回控制台上的用户。

link.ld:

OUTPUT_FORMAT("elf32-i386");
ENTRY(boot_start);

BOOTLOADER_BASE  = 0x7c00;
BOOTLOADER_RELOC = 0x600;
SECTOR_SIZE      = 512;
KERNEL_BASE      = BOOTLOADER_RELOC + SECTOR_SIZE;

SECTIONS
{
    __boot_reloc_addr = BOOTLOADER_RELOC;
    __boot_base_addr  = BOOTLOADER_BASE;
    __sector_sizew    = SECTOR_SIZE>>1;

    . = BOOTLOADER_RELOC;

    /* Code and data in boot.o placed between 0x7c00 and 0x7e00 */
    .boot : SUBALIGN(0) {
        boot.o(.text*)
        boot.o(.rodata*)
        boot.o(.data)
    }

    . = BOOTLOADER_RELOC + 0x200 - 2;
    /* Boot signature at 510th byte from beginning of bootloader's base */
    .sig : {
        SHORT(0xaa55);
    }

    KERNEL_ADJ = KERNEL_BASE - .;
    . = KERNEL_BASE;

    __disk_load_start = .;
    __disk_load_seg   = (__disk_load_start) >> 4;

    /* Kernel code and data */
    .kernel : AT(ADDR(.kernel) - KERNEL_ADJ) SUBALIGN(4) {
        *(.text*)
        *(.rodata*)
        *(.data)
    }
    __disk_load_end = .;
    __disk_load_num_sectors = (__disk_load_end - __disk_load_start + (SECTOR_SIZE - 1)) / SECTOR_SIZE;

    .kernel.bss : SUBALIGN(4) {
        __bss_start = .;
        *(COMMON);
        *(.bss)
        . = ALIGN(4);
        __bss_end = .;
    }
    __bss_sizew = SIZEOF(.kernel.bss)>>1;

    /* Remove unnecessary sections */
    /DISCARD/ : {
        *(.eh_frame);
        *(.comment);
    }
}

被屏蔽.Inc:

global bpb_disk_info

    jmp boot_start
    TIMES 3-($-$$) DB 0x90   ; Support 2 or 3 byte encoded JMPs before BPB.

bpb_disk_info:
    ; Dos 4.0 EBPB 1.44MB floppy
    OEMname:           db    "mkfs.fat"  ; mkfs.fat is what OEMname mkdosfs uses
    bytesPerSector:    dw    512
    sectPerCluster:    db    1
    reservedSectors:   dw    1
    numFAT:            db    2
    numRootDirEntries: dw    224
    numSectors:        dw    2880
    mediaType:         db    0xf0
    numFATsectors:     dw    9
    sectorsPerTrack:   dw    18
    numHeads:          dw    2
    numHiddenSectors:  dd    0
    numSectorsHuge:    dd    0
    driveNum:          db    0
    reserved:          db    0
    signature:         db    0x29
    volumeID:          dd    0x2d7e5a1a
    volumeLabel:       db    "NO NAME    "
    fileSysType:       db    "FAT12   "

boot.asm:

; These symbols are defined by the linker. We use them to zero BSS section
extern __bss_start
extern __bss_sizew

; These symbols are length (in sectors) of the kernel,
; and segment in memory to start reading to
extern __disk_load_num_sectors
extern __disk_load_seg

extern __sector_sizew;

; Mmory address to relocate the bootsector from / to
extern __boot_base_addr
extern __boot_reloc_addr

; This is the C entry point defined in kmain.c
extern kernelmain               ; kernelmain is C entry point
global boot_start               ; Make this global to suppress linker warning

KERNEL_LBA_START equ 1          ; Logical Block Address(LBA) kernel starts on
                                ;     LBA 1 = sector after boot sector
KERNEL_LBA_END   equ KERNEL_LBA_START + __disk_load_num_sectors
                                ; Logical Block Address(LBA) kernel ends at
DISK_RETRIES     equ 3          ; Number of times to retry on disk error

section .text
bits 16

; Include a BPB (1.44MB floppy with FAT12)
%include "bpb.inc"

boot_start:
    ; This code up until label .reloc must be position independent
    xor eax, eax                ; DS=0 since we use ORG 0x7c00. 0x0000<<4+0x7c00=0x7c00
    mov ds, ax
    mov es, ax
    mov ss, ax                  ; Stack at 0x0000:0x0000
    mov esp, eax                ; After first push will be 0x0000:0xfffe at top of 64kb

    ; Copy bootloader from  __boot_base_addr (0x7c00) to __boot_reloc_addr (0x600)
    ; We copy the bootloader to low memory above the BIOS Data Area (BDA) to allow
    ; more space for the kernel.
    cld
    mov cx, __sector_sizew
    mov si, __boot_base_addr
    mov di, __boot_reloc_addr
    rep movsw

    ; Jump to the relocated boot sector and set CS=0
    jmp 0x0000:.reloc
.reloc:

    ; Read kernel 1 sector at a time until kernel loaded
load_kernel:
    mov [bootDevice], dl        ; Save boot drive
    mov di, __disk_load_seg     ; DI = Current segment to read into
    mov si, KERNEL_LBA_START    ; SI = LBA that kernel starts at
    jmp .chk_for_last_lba       ; Check to see if we are last sector in kernel

.read_sector_loop:
    mov bp, DISK_RETRIES        ; Set disk retry count

    call lba_to_chs             ; Convert current LBA to CHS
    mov es, di                  ; Set ES to current segment number to read into
    xor bx, bx                  ; Offset zero in segment

.retry:
    mov ax, 0x0201              ; Call function 0x02 of int 13h (read sectors)
                                ;     AL = 1 = Sectors to read
    int 0x13                    ; BIOS Disk interrupt call
    jc .disk_error              ; If CF set then disk error

.success:
    add di, 512>>4              ; Advance to next 512 byte segment (0x20*16=512)
    inc si                      ; Next LBA

.chk_for_last_lba:
    cmp si, KERNEL_LBA_END      ; Have we reached the last kernel sector?
    jl .read_sector_loop        ;     If we haven't then read next sector

.kernel_loaded:
    jmp launch_kernel           ; Do realmode initialization and run kernel

.disk_error:
    xor ah, ah                  ; Int13h/AH=0 is drive reset
    int 0x13
    dec bp                      ; Decrease retry count
    jge .retry                  ; If retry count not exceeded then try again

error_end:
    ; Unrecoverable error; print drive error; enter infinite loop
    mov si, diskErrorMsg        ; Display disk error message
    call print_string
    cli
.error_loop:
    hlt
    jmp .error_loop

; Function: print_string
;           Display a string to the console on display page 0
;
; Inputs:   SI = Offset of address to print
; Clobbers: AX, BX, SI

print_string:
    mov ah, 0x0e                ; BIOS tty Print
    xor bx, bx                  ; Set display page to 0 (BL)
    jmp .getch
.repeat:
    int 0x10                    ; print character
.getch:
    lodsb                       ; Get character from string
    test al,al                  ; Have we reached end of string?
    jnz .repeat                 ;     if not process next character
.end:
    ret

;    Function: lba_to_chs
; Description: Translate Logical block address to CHS (Cylinder, Head, Sector).
;              Works for all valid FAT12 compatible disk geometries.
;
;   Resources: http://www.ctyme.com/intr/rb-0607.htm
;              https://en.wikipedia.org/wiki/Logical_block_addressing#CHS_conversion
;              https://stackoverflow.com/q/45434899/3857942
;              Sector    = (LBA mod SPT) + 1
;              Head      = (LBA / SPT) mod HEADS
;              Cylinder  = (LBA / SPT) / HEADS
;
;      Inputs: SI = LBA
;     Outputs: DL = Boot Drive Number
;              DH = Head
;              CH = Cylinder (lower 8 bits of 10-bit cylinder)
;              CL = Sector/Cylinder
;                   Upper 2 bits of 10-bit Cylinders in upper 2 bits of CL
;                   Sector in lower 6 bits of CL
;
;       Notes: Output registers match expectation of Int 13h/AH=2 inputs
;
lba_to_chs:
    push ax                     ; Preserve AX
    mov ax, si                  ; Copy LBA to AX
    xor dx, dx                  ; Upper 16-bit of 32-bit value set to 0 for DIV
    div word [sectorsPerTrack]  ; 32-bit by 16-bit DIV : LBA / SPT
    mov cl, dl                  ; CL = S = LBA mod SPT
    inc cl                      ; CL = S = (LBA mod SPT) + 1
    xor dx, dx                  ; Upper 16-bit of 32-bit value set to 0 for DIV
    div word [numHeads]         ; 32-bit by 16-bit DIV : (LBA / SPT) / HEADS
    mov dh, dl                  ; DH = H = (LBA / SPT) mod HEADS
    mov dl, [bootDevice]        ; boot device, not necessary to set but convenient
    mov ch, al                  ; CH = C(lower 8 bits) = (LBA / SPT) / HEADS
    shl ah, 6                   ; Store upper 2 bits of 10-bit Cylinder into
    or  cl, ah                  ;     upper 2 bits of Sector (CL)
    pop ax                      ; Restore scratch registers
    ret

; Set up segments so they are 0, zero out the BSS memory and transfer
; control to the function kernelmain
launch_kernel:
    xor ax, ax
    mov es, ax
    mov fs, ax
    mov gs, ax                  ; ES=FS=GS=0 (we set DS=SS=0 previously)

    ; We need to zero out the BSS section. We'll do it a WORD at a time
    mov edi, __bss_start        ; Start address of BSS
    mov ecx, __bss_sizew        ; Length of BSS in WORDS
                                ; Clear memory with value in AX (0x0000)
    rep stosw                   ; Do clear using string store instruction
                                ;     Clear 2 bytes at a time
    call dword kernelmain       ; Call kernel's "C" main entry point

.end_loop:                      ; Loop forever to terminate when kernel main is finished
    hlt
    jmp .end_loop


section .data
; Uncomment these lines if not using a BPB (via bpb.inc)
; numHeads:        dw 2         ; 1.44MB Floppy has 2 heads & 18 sector per track
; sectorsPerTrack: dw 18

bootDevice:      db 0x00
diskErrorMsg:    db "Unrecoverable disk error!", 0

kmain.c:

#include <stdint.h>

int getch()
{
   uint16_t inchar;

   __asm__ __volatile__ ("int $0x16\n\t"
                        : "=a"(inchar)
                        : "0"(0x0));

   return ((unsigned char)inchar);
}

/* getch that returns the scancode and not the ASCII character */
int getch_scancode()
{
   uint16_t inchar;

   /* upper 8 bits of inchar are the scancode in AH. */
   __asm__ __volatile__ ("int $0x16\n\t"
                        : "=a"(inchar)
                        : "0"(0x0));

   /* Shift right 8 bits to move scan code to the lower 8-bits */
   return ((unsigned char)(inchar>>8));
}

void printchar(int chr)
{
    /* AH=0x0e, AL=char to print, BH=page, BL=fg color */
    __asm__ __volatile__ ("int $0x10"
                          :
                          : "a" ((0x0e<<8) | (unsigned char)chr),
                            "b" (0x0000));
}

void printstring(char *str)
{
    while (*str)
        printchar (*str++);
}

/* Get NUL terminated string of maximum number of chars. The maximum
 * number of characters doesn't include the NULL terminator. Make sure the
 * str buffer passed can hold the maximum number characters plus an additional
 * byte for the NUL */
char *getstring(char *str, int maxnumchars)
{
    char inchar;
    int curpos = 0;

    /* Do nothing if NULL string or length is 0 */
    if (!maxnumchars || !str) return str;

    /* Continue string editing until ENTER (\r) is hit */
    while ((inchar = getch()) != '\r') {
        /* Process backspace, and do not allow backspacing past beginning of string.
         * Printing backspace using the BIOS is non-destructive. We must backspace,
         * print a space and then backspace once more to simulate a destructive
         * backspace */
        if (inchar == '\b') {
            if (curpos > 0) {
                curpos--;
                printstring("\b \b");
            }
            continue;
        }
        /* Toss away the tab character and do nothing */
        else if (inchar == '\t')
            continue;

        /* Store the keystroke pressed if we haven't reached end of buffer */
        if (curpos < maxnumchars) {
            str[curpos++] = inchar;
            printchar(inchar);
        }
    }
    /* Advance the cursor to the beginning of the next line with
     * Carriage return & Line Feed */
    printstring ("\r\n");
    /* Null terminate the string */
    str[curpos] = 0;

    return str;
}
    char str[41];

void kernelmain()
{
    /* Array to receive 40 characters + room for NUL terminator */

    printstring("\r\nEnter your name: ");
    getstring (str, sizeof(str)-1);
    printstring("Your name is: ");
    printstring(str);
    printstring("\r\n");
    return;
}

要编译/汇编和链接,您可以这样做:

nasm -f elf32 -Fdwarf -g boot.asm -o boot.o
i686-elf-gcc -g -c -m16 -ffreestanding -Os -Wall -fomit-frame-pointer kmain.c -o kmain.o
i686-elf-gcc -nostartfiles -nostdlib -Tlink.ld -o os.elf \
    boot.o kmain.o

# Convert os.elf to flat binary file os.bin
objcopy -Obinary os.elf os.bin

# Build 1.44MB disk image
dd if=/dev/zero of=disk.img bs=1024 count=1440
dd if=os.bin of=disk.img conv=notrunc

# Split the boot sector from the complete os.bin file
# These files may not be needed, generate them anyway
dd if=os.bin of=boot.bin bs=512 count=1
dd if=os.bin of=kernel.bin bs=512 seek=1

disk.img将是一个1.44MB的软盘映像,上面有bootloader和kernel。 boot.bin将是具有512字节引导扇区的二进制文件,而kernel.bin是内核。您可能不需要boot.binkernel.bin但我生成它们以防万一。

您应该能够在QEMU中运行它,如下所示:

qemu-system-i386 -fda disk.img

QEMU中的输出看起来类似于:

enter image description here


我建议使用cross compiler,但您可以修改上面的命令来编译/链接您的本机编译器。我不推荐它,但它应该工作:

gcc -fno-PIC -g -c -m16 -ffreestanding -Os -Wall -fomit-frame-pointer kmain.c -o kmain.o
ld -melf_i386 -nostartfiles -nostdlib -Tlink.ld -o os.elf \
    boot.o kmain.o
© www.soinside.com 2019 - 2024. All rights reserved.