2

I'm trying to boot a compressed kernel (vmlinuz) with a custom bootloader on a switch containing a MIPS 24KEc processor. I didn't write this bootloader, it is present in NOR flash on the product and the source code was provided by the vendor in their GPL archive.

The bootloader reads a header from a fixed location in flash containing:

  1. Header magic (SPIM)
  2. Kernel load address
  3. Length of data to load
  4. Kernel entry address

It then copies num_bytes following the header to the kernel load address and jumps to the entry address specified. The boot command line and initrd are compiled into the kernel image (via CONFIG_CMDLINE and CONFIG_INITRAMFS_SOURCE).

Uncompressed kernel images (vmlinux) boot normally. When I try to boot a compressed kernel, I do not have any output from the decompressor or kernel.

I noticed that the Linux kernel saves the argument registers before clearing BSS and jumping to the decompressor (from arch/mips/boot/compressed/head.S):

start:
    /* Save boot rom start args */
    move    s0, a0
    move    s1, a1
    move    s2, a2
    move    s3, a3

I am new to MIPS assembly, but I know that the above move statements are saving argument registers to saved registers. The saved registers are restored after the kernel is decompressed but before jumping to the kernel entry point:

    move    a0, s0
    move    a1, s1
    move    a2, s2
    move    a3, s3
    PTR_LI  k0, KERNEL_ENTRY

I looked at the u-boot source code and I'm wondering if this call in boot_jump_linux corresponds to the "boot rom start args" mentioned above:

kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
            linux_extra);

I could not find any kernel documentation explaining what arguments the kernel is expecting the boot rom to pass to it in registers a0, a1, a2, and a3.

6
  • 2
    Yes, that seems to be correct. Notice they get stored into fw_arg0..3 and then platform code accesses those, e.g. argc = fw_arg0;
    – Jester
    Apr 1, 2020 at 20:40
  • It might be worth trying this same bootloader + kernel in an emulator like QEMU, to see if it works there. You can connect to QEMU as a GDB remote and single-step / set breakpoints to see how things are supposed to work. The emulated machine might have different HW devices than the router, though, so testing early boot asm debug-print instructions in the kernel might be hard to be sure they should work on the real HW if reached. Apr 2, 2020 at 12:26
  • @PeterCordes I've tried loading the bootloader/kernel in qemu already, and unfortunately neither printed anything out to serial. I haven't tried qemu+gdb yet though. I suppose I could always modify the malta machine definition to have a uart port at the same address as the switch ASIC.
    – hwmartin
    Apr 2, 2020 at 13:39
  • Trying to find a problem in asm without a debugger is like trying to build a robot blindfolded. So many small-scale / local problems are easy to miss when staring at the source but very obvious when you can single-step and watch register values change. And especially on a large scale, having the only piece of information be "didn't work" makes it hard to narrow the search. vs. finding a loop you get stuck in, or a fault that happens can be huge. So yes IMO it's worth significant effort to make it possible to use a debugger even indirectly, and get something that works in a simulated machine Apr 2, 2020 at 14:30
  • Does the thing that works on the real HW (uncompressed kernel) also work in your QEMU guest? If so, then you have a known-good starting point to build from. Apr 2, 2020 at 14:31

1 Answer 1

2

The function prom_init_cmdline for your platform should implement reading the number of boot arguments (argc) from fw_arg0 and the boot arguments (argv) from fw_arg1. If this function is missing or empty, you will have to define it to receive the boot arguments from the bootloader.

The issue of no output turned out to be that the vendor kernel had no ability to read arguments from the bootloader (no prom_init_cmdline), and relied on boot arguments being compiled into the kernel via CONFIG_CMDLINE.

The reason I had no output from the kernel was that I did not compile the kernel with CONFIG_CMDLINE and thus it did not output to uart. I patched the vendor kernel to accept boot arguments passed by the bootloader in register a1 and got output on uart.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.