shortsking
Posts: 16
Joined: Sun Dec 27, 2020 2:37 pm

kernel.img wont boot

Mon Sep 27, 2021 9:30 pm

I was recently interested in Operating systems development, and found the [Baking Pi OS Development][/https://www.cl.cam.ac.uk/projects/raspb ... index.html] course. soon thereafter, i discovered that the tutorial was extremely outdated and meant for the RPi 1 model A, which is now unobtainable. i then found [this][/https://github.com/glennklockwood/raspb ... er/bmc2835], which is an example code similar to and based off of the other tutorial, but (i assume) less outdated. so i did all this, followed the tutorial and made some minor changes to my code. i am running this on an RPi zero.

i tried the build process, it worked, so then i tried to put my OS on the micro sd card. i tried the following procedures and NONE worked:
1. put the kernel.img in the raspberry pi imager and run it
2. put 109 nop instructions in the code to enlargen .img to 512 bytes so the imager will accept it and run.
3. put the kernel.img in the sd card with bootcode.bin, start.elf, config.txt, and itself.
4. replace just kernel.img in raspbian lite with my kernel

which yeilded the following results:
1. error: .img file is not a multiple of 512 bytes
2. operating system not reckognized by pi
3. operating system not reckognized
4. loads raspbian lite

what am i doing wrong??


cleverca22
Posts: 4742
Joined: Sat Aug 18, 2012 2:33 pm

Re: kernel.img wont boot

Tue Sep 28, 2021 4:31 am

kernel.img is not a disk image
it is a file that must live on a fat32 partition on the SD card

the other rpi firmware must also be on that fat32 partition, for the system to even boot

the pi0 uses the identical cpu to the pi1, so both can follow the same baremetal directions

shortsking
Posts: 16
Joined: Sun Dec 27, 2020 2:37 pm

Re: kernel.img wont boot

Tue Sep 28, 2021 11:48 pm

cleverca22 wrote:
Tue Sep 28, 2021 4:31 am
kernel.img is not a disk image
it is a file that must live on a fat32 partition on the SD card

the other rpi firmware must also be on that fat32 partition, for the system to even boot

the pi0 uses the identical cpu to the pi1, so both can follow the same baremetal directions

what other RPi firmware do i need? how would i obtain or program these pieces?

cleverca22
Posts: 4742
Joined: Sat Aug 18, 2012 2:33 pm

Re: kernel.img wont boot

Tue Sep 28, 2021 11:59 pm

shortsking wrote:
Mon Sep 27, 2021 9:30 pm
3. put the kernel.img in the sd card with bootcode.bin, start.elf, config.txt, and itself.
those are the main files you need, just skip the rpi imager entirely

you can also add enable_uart=1 and uart_2ndstage=1 to config.txt, to get more debug from the firmware, as its loading up your kernel

what asm are you using currently? there might be bugs in that as well

shortsking
Posts: 16
Joined: Sun Dec 27, 2020 2:37 pm

Re: kernel.img wont boot

Wed Sep 29, 2021 12:36 am

cleverca22 wrote:
Tue Sep 28, 2021 11:59 pm
shortsking wrote:
Mon Sep 27, 2021 9:30 pm
3. put the kernel.img in the sd card with bootcode.bin, start.elf, config.txt, and itself.
those are the main files you need, just skip the rpi imager entirely

you can also add enable_uart=1 and uart_2ndstage=1 to config.txt, to get more debug from the firmware, as its loading up your kernel

what asm are you using currently? there might be bugs in that as well

i am using either of the methods used on these sites:
1. https://www.cl.cam.ac.uk/projects/raspb ... /ok01.html
2. https://www.glennklockwood.com/embedded ... -gpio.html
though im pretty sure they have the same result and use gcc.

the result from that was just a color test screen, and no led turning on.

cleverca22
Posts: 4742
Joined: Sat Aug 18, 2012 2:33 pm

Re: kernel.img wont boot

Wed Sep 29, 2021 12:49 am

which gpio pin are you trying to turn on/off in the code?
which pin is the led on?
which model of pi are you using?

User avatar
rpdom
Posts: 19157
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: kernel.img wont boot

Wed Sep 29, 2021 7:35 am

With some of that code you might need "kernel_old=1" in config.txt if you kernel image loads at 0x0000 instead of 0x8000.

I did a fair bit of work with that code on my first Pi 1B. I should see if I can get it to work on a Zero.
Unreadable squiggle

shortsking
Posts: 16
Joined: Sun Dec 27, 2020 2:37 pm

Re: kernel.img wont boot

Mon Oct 04, 2021 9:33 pm

cleverca22 wrote:
Wed Sep 29, 2021 12:49 am
which gpio pin are you trying to turn on/off in the code?
which pin is the led on?
which model of pi are you using?

i am turning on (or rather, off) gpio pin 47.
i do not know for sure which pin the led is on
i am using the raspberry pi 0 w currently, though i plan to downgrade to a plane old pi 0

rpdom wrote: With some of that code you might need "kernel_old=1" in config.txt if you kernel image loads at 0x0000 instead of 0x8000.

I did a fair bit of work with that code on my first Pi 1B. I should see if I can get it to work on a Zero.

i added that line to config.txt, and it didn't change a thing.

cleverca22
Posts: 4742
Joined: Sat Aug 18, 2012 2:33 pm

Re: kernel.img wont boot

Mon Oct 04, 2021 9:40 pm

shortsking wrote:
Mon Oct 04, 2021 9:33 pm
i am turning on (or rather, off) gpio pin 47.
i do not know for sure which pin the led is on
checking my notes, gpio47 is the green status led
for pi0, pi0w, some of the pi1, and pi2

so you should see something, if your accessing the pin right

can you post the full asm file your using? and maybe include the kernel.img as well (add it to a zip, and the forum will allow it as an attachment)

shortsking
Posts: 16
Joined: Sun Dec 27, 2020 2:37 pm

Re: kernel.img wont boot

Mon Oct 04, 2021 9:55 pm

cleverca22 wrote:
Mon Oct 04, 2021 9:40 pm
shortsking wrote:
Mon Oct 04, 2021 9:33 pm
i am turning on (or rather, off) gpio pin 47.
i do not know for sure which pin the led is on
checking my notes, gpio47 is the green status led
for pi0, pi0w, some of the pi1, and pi2

so you should see something, if your accessing the pin right

can you post the full asm file your using? and maybe include the kernel.img as well (add it to a zip, and the forum will allow it as an attachment)
here is my program (without comments):

Code: Select all

.section .init
.globl _start
_start:


mov r1, #1
lsl r1, #21

str r1, [r0, #16]


mov r1, #1

lsl r1, #15

str r1, [r0, #32]

loop$:
b loop$
my browser refused to link the .img file

cleverca22
Posts: 4742
Joined: Sat Aug 18, 2012 2:33 pm

Re: kernel.img wont boot

Mon Oct 04, 2021 10:02 pm

shortsking wrote:
Mon Oct 04, 2021 9:55 pm
mov r1, #1
lsl r1, #21

str r1, [r0, #16]
this will put a 1 into r1
then it will shift it to the left by (7*3)

then it saves it to r0 + 16
what is r0? you never wrote to that register!
shortsking wrote:
Mon Oct 04, 2021 9:55 pm
my browser refused to link the .img file
cleverca22 wrote:
Mon Oct 04, 2021 9:40 pm
add it to a zip, and the forum will allow it as an attachment

shortsking
Posts: 16
Joined: Sun Dec 27, 2020 2:37 pm

Re: kernel.img wont boot

Thu Oct 14, 2021 11:04 pm

i changed the code so now the asm file looks like this:

Code: Select all

.section .init
.globl _start
_start:

/* Store 0x20200000 in r0; this is the base address of the GPIO controller */
ldr r0, =0x20200000

/*
 * Enable output to GPIO pin 47 (the ACT LED) by flipping a bit in a specific
 * register in the GPIO controller.
 */
mov r1, #1  /* r1 = 00000000 00000000 00000000 00000001 */

lsl r1, #21 /* r1 = 00000000 00100000 00000000 00000000 */

str r1, [r0, #16]  /* write contents of r1 into address given by r0 + 16 (GPFSEL4) */

mov r1, #1  /* 00000000 00000000 00000000 00000001 */

lsl r1, #15 /* 00000000 00000000 10000000 00000000 */

str r1, [r0, #32] /* write contents of r1 into address given by r0 + 32 */

/*
 * Loop forever
 */
loop$:
b loop$
but the ACT LED still doesnt light up. i know its booting because it does the standard color test for programming the GPU.

and the .img file is too large to post.

cleverca22
Posts: 4742
Joined: Sat Aug 18, 2012 2:33 pm

Re: kernel.img wont boot

Thu Oct 14, 2021 11:15 pm

can you post a photo of your rpi board?

shortsking
Posts: 16
Joined: Sun Dec 27, 2020 2:37 pm

Re: kernel.img wont boot

Sat Oct 16, 2021 7:21 pm

FullSizeRender (1).jpg
FullSizeRender (1).jpg (37.74 KiB) Viewed 477 times

cleverca22
Posts: 4742
Joined: Sat Aug 18, 2012 2:33 pm

Re: kernel.img wont boot

Sun Oct 17, 2021 12:10 am

shortsking wrote:
Thu Oct 14, 2021 11:04 pm

Code: Select all

mov r1, #1  /* 00000000 00000000 00000000 00000001 */
lsl r1, #15 /* 00000000 00000000 10000000 00000000 */
str r1, [r0, #32] /* write contents of r1 into address given by r0 + 32 */
thats writing to GP_SET1, so it should be turning gpio47 to high
but i dont remember if the led is active high or active low

what if you instead use GP_CLR1, at 0x0x2020002c

shortsking
Posts: 16
Joined: Sun Dec 27, 2020 2:37 pm

Re: kernel.img wont boot

Sun Oct 17, 2021 11:44 pm

cleverca22 wrote:
Sun Oct 17, 2021 12:10 am
shortsking wrote:
Thu Oct 14, 2021 11:04 pm

Code: Select all

mov r1, #1  /* 00000000 00000000 00000000 00000001 */
lsl r1, #15 /* 00000000 00000000 10000000 00000000 */
str r1, [r0, #32] /* write contents of r1 into address given by r0 + 32 */
thats writing to GP_SET1, so it should be turning gpio47 to high
but i dont remember if the led is active high or active low

what if you instead use GP_CLR1, at 0x0x2020002c
i added this to the beginning of my code:

Code: Select all

ldr r1, =0x2020002c
and changed

Code: Select all

str r1,[r0,#32]
to:

Code: Select all

ldr r2, =0x2020002c
but the led still wouldn't turn on

despite these failures, i got the .img file to attach!!!
kernel.img.zip
(212 Bytes) Downloaded 9 times

cleverca22
Posts: 4742
Joined: Sat Aug 18, 2012 2:33 pm

Re: kernel.img wont boot

Mon Oct 18, 2021 2:59 am

Code: Select all

pi@pi400:~ $ objdump -b binary -m arm -D  kernel.img 

kernel.img:     file format binary


Disassembly of section .data:

00000000 <.data>:
   0:   e59f001c        ldr     r0, [pc, #28]   ; 0x24
   4:   e59f201c        ldr     r2, [pc, #28]   ; 0x28
   8:   e3a01001        mov     r1, #1
   c:   e1a01a81        lsl     r1, r1, #21
  10:   e5801010        str     r1, [r0, #16]
  14:   e3a01001        mov     r1, #1
  18:   e1a01781        lsl     r1, r1, #15
  1c:   e5802020        str     r2, [r0, #32]
  20:   eafffffe        b       0x20
  24:   20200000        eorcs   r0, r0, r0
  28:   2020002c        eorcs   r0, r0, ip, lsr #32
using this, i can confirm its not anything wrong with the assembler or linker flags

since its a raw binary file, objdump doesnt know that 0x24/0x28 are .data, so youll just want to read those as ints yourself

1c: e5802020 str r2, [r0, #32]
and this i think is backwards
you want "str r1, [r2]" to write to the clear register

shortsking
Posts: 16
Joined: Sun Dec 27, 2020 2:37 pm

Re: kernel.img wont boot

Mon Oct 18, 2021 8:51 pm

sorry for the mistake, but I fixed it and it still doesn't work

Return to “Bare metal, Assembly language”