From 7cff2a3fc93a636f8e253892af212a30c5a58697 Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Sun, 6 Sep 2015 23:16:58 +0200 Subject: [PATCH] Factor out precefaces and postfixes, apm_shutdown2 --- Makefile | 5 ++- README.md | 14 +++++++ shutdown_apm.S => apm_shutdown.S | 9 ++-- apm_shutdown2.S | 70 ++++++++++++++++++++++++++++++++ bios_hello_world.S | 6 +-- bios_one_char.S | 6 +-- common.h | 3 ++ 7 files changed, 101 insertions(+), 12 deletions(-) rename shutdown_apm.S => apm_shutdown.S (94%) create mode 100644 apm_shutdown2.S create mode 100644 common.h diff --git a/Makefile b/Makefile index ff49fa2..92fd323 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,8 @@ BITS ?= 32 IN_EXT ?= .S LD ?= ld -MYAS ?= as +# Use gcc so that the preprocessor will run first. +MYAS ?= gcc OBJ_EXT ?= .o OUT_EXT ?= .img RUN ?= bios_hello_world @@ -22,7 +23,7 @@ all: $(OUTS) $(LD) --oformat binary -o '$@' '$<' -T a.ld #-Ttext 0x7C00 %$(OBJ_EXT): %$(IN_EXT) - $(MYAS) -o '$@' '$<' + $(MYAS) -c -o '$@' '$<' clean: rm -f *$(OBJ_EXT) *$(OUT_EXT) diff --git a/README.md b/README.md index c521220..ac34b30 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,20 @@ You cannot use any libraries, so how to do IO? Some ways that this can be done: - - VBE +Showdown and restart can be managed with either: + +- ACPI + + Newer and better. + +- APM + + + + Older and simpler. + +See also: + ## Bibliography - diff --git a/shutdown_apm.S b/apm_shutdown.S similarity index 94% rename from shutdown_apm.S rename to apm_shutdown.S index 2994b73..aad65ca 100644 --- a/shutdown_apm.S +++ b/apm_shutdown.S @@ -1,5 +1,3 @@ -.code16 - /* http://wiki.osdev.org/Shutdown @@ -8,6 +6,10 @@ http://stackoverflow.com/questions/678458/shutdown-the-computer-using-assembly http://stackoverflow.com/questions/3145569/how-to-power-down-the-computer-from-a-freestanding-environment */ +#include "common.h" + +BEGIN + mov $0x5301, %ax xor %bx, %bx int $0x15 @@ -24,5 +26,4 @@ mov $0x0001, %bx mov $0x0003, %cx int $0x15 -.org 510 -.word 0xaa55 +END diff --git a/apm_shutdown2.S b/apm_shutdown2.S new file mode 100644 index 0000000..495d90e --- /dev/null +++ b/apm_shutdown2.S @@ -0,0 +1,70 @@ +/* +Example from: http://wiki.osdev.org/APM + +Is all of this really needed? Compare to apm_shutdown.S. +*/ + +#include "common.h" + +BEGIN + +movb $0x53,%ah #this is an APM command +movb $0x0,%al #installation check command +xorw %bx,%bx #device id (0 = APM BIOS) +int $0x15 #call the BIOS function through interrupt 15h +jc APM_error #if the carry flag is set there was an error + #the function was successful + #AX = APM version number + #AH = Major revision number (in BCD format) + #AL = Minor revision number (also BCD format) + #BX = ASCII characters "P" (in BH) and "M" (in BL) + #CX = APM flags (see the official documentation for more details) + +#disconnect from any APM interface +movb $0x53,%ah #this is an APM command +movb $0x4,%al #interface disconnect command +xorw %bx,%bx #device id (0 = APM BIOS) +int $0x15 #call the BIOS function through interrupt 15h +jc .disconnect_error #if the carry flag is set see what the fuss is about. +jmp .no_error + +.disconnect_error: #the error code is in ah. +cmpb $0x3,%ah #if the error code is anything but 03h there was an error. +jne APM_error #the error code 03h means that no interface was connected in the first place. + +.no_error: + #the function was successful + #Nothing is returned. + +#connect to an APM interface +movb $0x53,%ah #this is an APM command +movb $0x01,%al #see above description +xorw %bx,%bx #device id (0 = APM BIOS) +int $0x15 #call the BIOS function through interrupt 15h +jc APM_error #if the carry flag is set there was an error + #the function was successful + #The return values are different for each interface. + #The Real Mode Interface returns nothing. + #See the official documentation for the + #return values for the protected mode interfaces. + +#Enable power management for all devices +movb $0x53,%ah #this is an APM command +movb $0x8,%al #Change the state of power management... +movw $0x001,%bx #...on all devices to... +movw $0x001,%cx #...power management on. +int $0x15 #call the BIOS function through interrupt 15h +jc APM_error #if the carry flag is set there was an error + +#Set the power state for all devices +movb $0x53,%ah #this is an APM command +movb $0x07,%al #Set the power state... +movw $0x0001,%bx #...on all devices to... +movw $0x0003,%cx #see above +int $0x15 #call the BIOS function through interrupt 15h +jc APM_error #if the carry flag is set there was an error + +APM_error: +hlt + +END diff --git a/bios_hello_world.S b/bios_hello_world.S index 746fc73..d5a00a4 100644 --- a/bios_hello_world.S +++ b/bios_hello_world.S @@ -1,4 +1,5 @@ -.code16 +#include "common.h" +BEGIN cli mov $msg, %si mov $0x0e, %ah @@ -12,5 +13,4 @@ halt: hlt msg: .asciz "hello world" -.org 510 -.word 0xaa55 +END diff --git a/bios_one_char.S b/bios_one_char.S index 55926b1..0ab070a 100644 --- a/bios_one_char.S +++ b/bios_one_char.S @@ -1,10 +1,10 @@ # Print a single `@` character with the BIOS. # More minimal than the hello world. -.code16 +#include "common.h" +BEGIN cli mov $0x40, %al mov $0x0e, %ah int $0x10 hlt -.org 510 -.word 0xaa55 +END diff --git a/common.h b/common.h new file mode 100644 index 0000000..ae54dcd --- /dev/null +++ b/common.h @@ -0,0 +1,3 @@ +#define BEGIN .code16 +#define END .org 510;\ + .word 0xaa55