magic constants in btxldr/btxcsu

Andriy Gapon avg at FreeBSD.org
Mon Sep 19 13:49:52 UTC 2011


I was looking at how btx works and in particular how arguments are passed from
boot2-ish to loader.  It seems that the code that implements that logic uses a few
integer literals which make the code not quite obvious.

For example:
                movl $0x48,%ecx                 # Allocate space
                subl %ecx,%ebp                  #  for bootinfo
                movl 0x18(%esp,1),%esi          # Source: bootinfo
                cmpl $0x0, %esi                 # If the bootinfo pointer
                je start_null_bi                #  is null, don't copy it
                movl %ebp,%edi                  # Destination
                rep                             # Copy
                movsb                           #  it
                movl %ebp,0x18(%esp,1)          # Update pointer
#ifdef BTXLDR_VERBOSE
                movl $m_rel_bi,%esi             # Display
                movl %ebp,%eax                  #  bootinfo
                call hexout                     #  relocation
                call putstr                     #  message
#endif
start_null_bi:  movl $0x18,%ecx                 # Allocate space
                subl %ecx,%ebp                  #  for arguments
                leal 0x4(%esp,1),%esi           # Source
                movl %ebp,%edi                  # Destination
                rep                             # Copy
                movsb                           #  them


Once I looked at this code I was able to understand how things work, but finding
this code was not easy.

So, apparently 0x48 should be a size of bootinfo; 0x18(%esp,1) should be a magic
offset of bootinfo pointer in the args on the stack; 0x18 should be a combined
size of arguments; 0x4(%esp,1),%esi should be a start of the arguments.

Then, there is this:
	.set ARGADJ,0xfa0               # Argument adjustment

It seems that the magic value is constructed as 0x1000 - (0x48 + 0x18), where 0x48
and 0x18 come from the previous snippet.  But this is not obvious again.

I am not fluent with that code at all to suggest any changes.
But maybe these constants could be defined more prominently and somehow bound
together.

-- 
Andriy Gapon


More information about the freebsd-hardware mailing list