Author Topic: Using ld & dd for bin programs  (Read 14123 times)

Offline Deskman243

  • Jr. Member
  • *
  • Posts: 49
Using ld & dd for bin programs
« on: November 21, 2022, 11:39:54 PM »
Greetings chat glad you're here

I'm continuing a program platform from my previous post here.
https://forum.nasm.us/index.php?topic=2880.0

To summarize I made modules for LBA and kernel files using the methods I previously outlined and I'm looking into programming from protected mode where I found applications for VESA linear buffers.

Now I'm trying to fix the new bin file for VESA because this was not being addressed the same way that the kernel file was. In fact the address is simply being shifted in front of the kernel contrary to the linker settings.

Originally I could not get the VESA data displayed until I changed the LBA module's length to precisely fit the program (I.E. reducing the size of the full file by the size of the Vesa file).

Here is where I wanted to show the linker files and Makefile I'm using.
Any counsel or clue would be really good here Thank you.

Code: [Select]
OUTPUT_FORMAT(binary)
OUTPUT_ARCH(i386)
ENTRY(_prep_module)

SECTIONS
{
        vesaOutput 0x7E00 : {
            vesa.o
            _mode_block = 0x7E00
        }

. = 0x7c00;
_premier = .;
_prep_module =.;

.text : { *(.text)}
.rodata : {*(.rodata)}
.data : {*(.data)}


. = _premier + 512*1-2;


.dummy : {BYTE(0x55); BYTE(0xaa);}



_bss_premier =.;
.bss : {*(.bss)}
_bss_stub = .;
_heap = .;

/DISCARD/ : {*(.comment) *(.eh_frame) *(.note.GNU-stack)}


}


Code: [Select]
FILES = boot.o printstr.o vesa.o
LINK= kernel.o

all: boot.bin kernelclean.bin vesa.bin
rm -rf osprep.bin
rm -rf os.bin
dd if=boot.bin >> osprep.bin
dd if=vesa.bin >> osprep.bin
# cat osprep.bin vb.bin > os.bin
cp -r osprep.bin os.bin
dd if=kernelclean.bin >> os.bin
dd if=/dev/zero bs=512 count=98 >> os.bin
dd if=/dev/zero bs=256 count=1 >> os.bin

boot.o: boot.asm
nasm -f elf -g -o $@ $<

printstr.o: printstr.asm
nasm -f elf -g -o $@ $<

kernel.o: kernel.asm
nasm -f elf -g -o kernel.o kernel.asm

vesa.o: vb.asm
nasm -f elf -g -o vesa.o vb.asm

vb.bin: vb.asm
nasm -f bin -o vb.bin vb.asm

kernel.bin: kernel.asm
nasm -f bin -g -o kernel.bin kernel.asm

vesa.bin: vesa.o
ld -m elf_i386 -g -Ttext 0x7E00 -o vesa.bin vesa.o --oformat binary -e _mode_block
# ld -T vesa.ld vesa.o -o vesa.bin -e _mode_block
# ld -m elf_i386 -g -Ttext 0x00007E00 -o vesa.bin vesa.o --oformat binary -e _mode_block
# ld -T vesa.ld vesa.o -o vesa.bin -e _mode_block

boot.bin: $(FILES)
ld -T boot.ld $(FILES) -o boot.bin

kernelclean.bin: kernel.o
ld -m elf_i386 -g -Ttext 0x0100000 -o kernelclean.bin kernel.o --oformat binary -e _kernel_module
# ld -g -relocatable -T link.ld -o kernel.bin kernel.o
# ld -m elf_i386 -g -Ttext 0x0100000 -o kernel.bin kernel.o
# ld -g -T link.ld kernel.o -o kernel.bin
# ld -m elf_i386 -g -Ttext 0x0100000 kernel.o -o kernel.bin --oformat binary
# ld -g -relocatable -T link.ld kernel.o -o kernel.bin



clean:
rm -rf boot.bin
rm -rf boot.o
rm -rf printstr.o
rm -rf kernel.o
rm -rf vesa.o
rm -rf kernelclean.bin
rm -rf vesa.bin
rm -rf vb.bin


and here's the main files for reference
boot.asm

Code: [Select]
bits 16

extern printstr

section .data

value1: dw 0
address1: dw 0
address2: dw 0


section .text


KERNEL_SECTION equ 0x0100000

global _prep_module
_prep_module:
push cs
pop ds
mov ax,ds
mov [value1],ax

cld


jmp short prep_module
nop
times 33 db 0

prep_module:
jmp 0:module1

module1:
cli
mov ax,0x00
mov ds,ax
mov es,ax

mov ax,0x00
mov ss,ax
; mov sp,0x7c00
mov sp,0x8000

sti

mov cx,[0x7E00]

lea si,[msg]
call printstr


RM_module:
mov eax,0
mov ax,$+6
mov [address1],ax

; jmp load_protected

load_protected:
cli
lgdt[gdt_table]
mov eax,cr0
or eax,0x1
mov cr0,eax

jmp CODE_SECTION:load32


[bits 32]

load32:


mov eax,1
mov ecx,100
mov esi,0x0100000
call ata_lba_read


mov ax,DATA_SECTION
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
mov ss,ax
mov ebp,0x90000
mov esp,ebp


push ebp
pop eax

call placer1_buffer



in al,0x92
or al,2
out 0x92,al

jmp CODE_SECTION:0x0100000

placer1_buffer:

add eax,4
mov ebp,esp
mov esp,eax


mov ebx,0
mov bx,[address1]
push bx

mov ebx,0
mov bx,[address2]
push bx

mov esp,ebp


mov ebp,eax

ret


ata_lba_read:
mov ebx,eax
shr eax,24
or eax,0xE0
mov dx,0x1f6
out dx,al

mov eax,ecx
mov dx,0x1f2
out dx,al

mov eax,ebx
mov dx,0x1f3
out dx,al

mov dx,0x1f4
mov eax,ebx
shr eax,8
out dx,al

mov dx,0x1f5
mov eax,ebx
shr eax,16
out dx,al

mov dx,0x1f7
mov al,0x20
out dx,al

.continue_sect:
push ecx

.try_again:
mov dx,0x1f7
in al,dx
test al,8
jz .try_again
mov ecx,256

.lba_loop:
mov dx,0x1f0
in ax,dx
mov [es:esi],ax
add esi,2

;pop ecx
dec ecx
cmp ecx,0
jnz .lba_loop


pop ecx
loop .continue_sect

mov ax,$+25
mov [address2],ax

ret

disk_check:
lea si,[msg]
call printstr
jmp $

[bits 16]

RM_revert:

pop bx
mov esp,0x00008000

[bits 16]

clc

mov eax,cr0
and eax,0ffffffeh
mov cr0,eax


mov esi,0


push address1
pop bx


; mov esi,[ebx]

mov cx,[bx]
mov si,bx

; mov si,[address2]

jc Color_map

jmp si

Color_map:
jmp Color_map


.halt:
hlt
jmp .halt

%include 'gdt.inc'

;times 510 - ($-$$) db 0
;dw 0xaa55

section .rodata

msg: db 'test string',0


buffer:






vb.asm
Code: [Select]
bits 16

;times 512 -($-$$) db 0

;section .data
;section .text
global _mode_block
_mode_block: times 256 db 0x11

stack: times 256 dd 0
stackplacer:

;section .text



;times 512 -($-$$) db 0

« Last Edit: November 22, 2022, 09:33:21 PM by Deskman243 »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Using ld for bin programs
« Reply #1 on: November 22, 2022, 07:45:00 PM »
I'm glad you're here, too. Nice break from the spam!

Generally, bin files are not linked !

Back in the day... when I did boot sectors, I had a floppy drive and wrote 'em to a floppy disk. You seem to have a different problem. You seem to know what you're doing... but bin files are not linked, Using ld is not strictly a Nasm question. "elf" files are, in my mind, something completely different. I don't see how you're using them. With the right linker script, you MIGHT be able to use them... maybe... More information on what you're doing MIGHT help us help you... maybe...
For starters, what are those ports you're doing "in" and "out" to? I'm feeling pretty clueless these days. so I probably can't help you much...

 Good luck.

Best,
Frank


Offline Deskman243

  • Jr. Member
  • *
  • Posts: 49
Re: Using ld for bin programs
« Reply #2 on: November 22, 2022, 09:32:46 PM »
Hmm well generally you can convert programs into different types like bin or elf. I guess what you want to know is how are these files not similar. The type of file reflects the actual binary. That's why bin files are common for programming models. To your other question the LBA module uses the ATA PIO standard for configurations.

Here is a link to a good post if you would like to read more about the topic.
https://wiki.osdev.org/ATA_PIO_Mode

What I really want to know here is why does combining bin files ignore the dd sequence in the Makefile. Here I have tried 2 different sequences however the dd if=/dev/zero bs=512 count=98 keeps vesa.bin in front of kernel.bin when they are called from before.
« Last Edit: November 22, 2022, 09:38:05 PM by Deskman243 »

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 224
  • Country: gb
    • GPUZoo
Re: Using ld for bin programs
« Reply #3 on: November 22, 2022, 11:24:12 PM »
I'm glad you're here, too. Nice break from the spam!

Generally, bin files are not linked !

Back in the day... when I did boot sectors, I had a floppy drive and wrote 'em to a floppy disk. You seem to have a different problem. You seem to know what you're doing... but bin files are not linked, Using ld is not strictly a Nasm question. "elf" files are, in my mind, something completely different. I don't see how you're using them. With the right linker script, you MIGHT be able to use them... maybe... More information on what you're doing MIGHT help us help you... maybe...
For starters, what are those ports you're doing "in" and "out" to? I'm feeling pretty clueless these days. so I probably can't help you much...

 Good luck.

Best,
Frank

LOL about the spam. It's gotten way out of hand lately. Glad it's not just me removing it :)

Port 092h is generally used to enable/disable the #A20 line, for entering and leaving protected mode.
My graphics card database: www.gpuzoo.com

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Using ld & dd for bin programs
« Reply #4 on: November 22, 2022, 11:41:48 PM »
Hi Debs,
Thanks!

Great link, Deskman243! I will have to study it.

Best,
Frank