hi3516av300 bootrom — annotated hex window
============================================

Generated from `make disasm` against the dump matching MANIFEST. Reproduce
with:

  bootrom/scripts/dump.sh openipc-hi3516av300.dlab.doty.ru hi3516av300
  bootrom/scripts/disasm.sh hi3516av300

Below is hand-annotated coverage of bytes 0x00000000..0x000001ff.

Vector table
------------

  04000000: 10 00 00 ea     b      0x4000048           ; reset → _start
  04000004: 14 f0 9f e5     ldr pc, [pc, #20]          ; → *(0x4000020) = 0x100
  04000008: 14 f0 9f e5     ldr pc, [pc, #20]          ; → *(0x4000024) = 0x100
  0400000c: 14 f0 9f e5     ldr pc, [pc, #20]          ; → *(0x4000028) = 0x100
  04000010: 14 f0 9f e5     ldr pc, [pc, #20]          ; → *(0x400002c) = 0x100
  04000014: 14 f0 9f e5     ldr pc, [pc, #20]          ; → *(0x4000030) = 0x100
  04000018: 14 f0 9f e5     ldr pc, [pc, #20]          ; → *(0x4000034) = 0x100
  0400001c: 14 f0 9f e5     ldr pc, [pc, #20]          ; → *(0x4000038) = 0x100
  04000020: 00 01 00 00     .word  0x00000100          ; exception-1 target
  04000024: 00 01 00 00     .word  0x00000100          ; exception-2 target
  04000028: 00 01 00 00     .word  0x00000100
  0400002c: 00 01 00 00     .word  0x00000100
  04000030: 00 01 00 00     .word  0x00000100
  04000034: 00 01 00 00     .word  0x00000100
  04000038: 00 01 00 00     .word  0x00000100
  0400003c: 78 56 34 12     .word  0x12345678         ; _magic
  04000040: 9c 00 00 04     .word  0x0400009c         ; clear_remap_rom_ptr → clear_remap_start
  04000044: 00 65 01 04     .word  0x04016500         ; clear_remap_ptr → SDRAM destination

_start
------

  04000048: e10f0000        mrs    r0, CPSR
  0400004c: e3c0001f        bic    r0, r0, #0x1f         ; clear mode bits
  04000050: e38000d3        orr    r0, r0, #0xd3         ; supervisor, IRQ+FIQ masked
  04000054: e129f000        msr    CPSR_fc, r0
  04000058: eb0009f9        bl     0x4002844             ; disable_ctrl_features
  0400005c: e59f00ac        ldr    r0, [pc, #172]        ; r0 = STACK_START (0x04010500)
  04000060: e1a0d000        mov    sp, r0
  04000064: eb000035        bl     0x4000140             ; configure_soc_clocks
  04000068: e59f00a4        ldr    r0, [pc, #164]        ; r0 = SDRAM_START (0x04010000)
  0400006c: e3a01000        mov    r1, #0
  04000070: e59f20a0        ldr    r2, [pc, #160]        ; r2 = 0x04010268
  04000074: e0522000        subs   r2, r2, r0            ; r2 = 0x268
  04000078: eb000a20        bl     0x4002900             ; memset(SDRAM_START, 0, 0x268)
  0400007c: eb0009a0        bl     0x4002704             ; main()
  04000080: e51f0048        ldr    r0, [pc, #-72]        ; clear_remap_rom_ptr = 0x0400009c
  04000084: e51f1048        ldr    r1, [pc, #-72]        ; clear_remap_ptr     = 0x04016500
  04000088: e59f208c        ldr    r2, [pc, #140]        ; r2 = clear_remap_start (0x9c)
  0400008c: e59f308c        ldr    r3, [pc, #140]        ; r3 = clear_remap_end   (0xe0)
  04000090: e0432002        sub    r2, r3, r2            ; r2 = 0x44 bytes
  04000094: eb000011        bl     0x40000e0             ; memcpy32
  04000098: e51ff05c        ldr    pc, [pc, #-92]        ; pc = clear_remap_ptr = 0x04016500

clear_remap thunk (copied to SDRAM, executed there)
----------------------------------------------------

  0400009c: e59f4038        ldr    r4, [pc, #56]         ; r4 = SYSCTRL_START = 0x12020000
  040000a0: e5940000        ldr    r0, [r4]              ; SC_CTRL
  040000a4: e3800c01        orr    r0, r0, #0x100        ; bit 8: clear boot remap
  040000a8: e5840000        str    r0, [r4]
  040000ac: e59f1024        ldr    r1, [pc, #36]         ; r1 = SDRAM_START
  040000b0: e5910000        ldr    r0, [r1]              ; r0 = *(SDRAM_START) — entry point
  040000b4: e1a0f000        mov    pc, r0
  040000b8-040000d4:        e320f000              ; 7× nop
  040000d8: 00 00 01 04     .word  0x04010000          ; SDRAM_START literal
  040000dc: 00 00 02 12     .word  0x12020000          ; SYSCTRL_START literal

memcpy32 (LDM/STM 8-register block copy)
-----------------------------------------

  040000e0: e0802002        add    r2, r0, r2            ; r2 = src + len
  040000e4: e8b007f8        ldm    r0!, {r3-r10}         ; load 32 bytes
  040000e8: e8a107f8        stmia  r1!, {r3-r10}         ; store 32 bytes
  040000ec: e1500002        cmp    r0, r2
  040000f0: dafffffb        ble    0x40000e4             ; loop
  040000f4: e1a0f00e        mov    pc, lr

  Note: copies in 32-byte chunks, overshoots by up to 31 bytes if size
  isn't a multiple of 32. The clear_remap thunk is exactly 0x44 (68)
  bytes = 2 chunks of 32 + 4 of overshoot — caller pads safely.

_dead_loop (exception fallback)
--------------------------------

  04000100: e59f101c        ldr    r1, [pc, #28]         ; r1 = SYSCTRL_START
  04000104: e3a00001        mov    r0, #1
  04000108: e5810004        str    r0, [r1, #4]          ; SC_SYSRES: soft reset
  0400010c: eafffffe        b      0x400010c             ; spin until reset

Literal pool for _start
------------------------

  04000110: 00 05 01 04     .word  0x04010500   ; STACK_START
  04000114: 00 00 01 04     .word  0x04010000   ; SDRAM_START
  04000118: 68 02 01 04     .word  0x04010268   ; end of SDRAM clear region
  0400011c: 9c 00 00 00     .word  0x0000009c   ; clear_remap_start offset
  04000120: e0 00 00 00     .word  0x000000e0   ; clear_remap_end offset

configure_soc_clocks
---------------------

  04000140: e3a01000        mov    r1, #0
  04000144: e3012389        movw   r2, #0x1389           ; timeout = 5001 iters
  04000148: e3411201        movt   r1, #0x1201           ; r1 = CRG_START = 0x12010000
  0400014c: ea000006        b      0x400016c             ; skip NOP block on first iter
  04000150-04000164:        e320f000             ; 6× nop — inter-poll delay
  04000164: e2522001        subs   r2, r2, #1
  04000168: 0a000003        beq    0x400017c             ; timeout fall-through
  0400016c: e59131e8        ldr    r3, [r1, #0x1e8]      ; r3 = CRG.PLL_LOCK_STAT
  04000170: e203300b        and    r3, r3, #0xb          ; mask: APLL+DPLL+EPLL lock bits
  04000174: e353000b        cmp    r3, #0xb              ; all three locked?
  04000178: 1afffff4        bne    0x4000150             ; loop with delay if not

  0400017c: e3a01000        mov    r1, #0
  04000180: e3a0300a        mov    r3, #10               ; post-switch settle counter
  04000184: e3411201        movt   r1, #0x1201           ; r1 = CRG_START
  04000188: e5912080        ldr    r2, [r1, #0x80]       ; r2 = CRG.SOC_CONFIG (PERI_CRG32)
  0400018c: e3c22e5f        bic    r2, r2, #0x5f0        ; clear bits 4-8 + bit 10
  04000190: e3c2200f        bic    r2, r2, #0xf          ; clear bits 0-3
  04000194: e3822e59        orr    r2, r2, #0x590        ; set bits 4,7,8,10
  04000198: e3822002        orr    r2, r2, #0x2          ; set bit 1
  0400019c: e5812080        str    r2, [r1, #0x80]       ; CRG.SOC_CONFIG: post-mask bits 1/4/7/8/10 set

  040001a0-040001a8:        e320f000 / subs/bne   ; 10× nop settle
  040001ac: e12fff1e        bx     lr

  Resulting SOC_CONFIG: bits 0/2/3/5/6 cleared, 1/4/7/8/10 set, bit 9
  and bits ≥11 preserved. Subfield semantics (which sub-PLL feeds each
  domain, divider ratios) await cross-reference against the av300
  vendor user-guide.

  Re-compiled freestanding bootloader.c matches this disassembly
  instruction-for-instruction.

reset_soc_clocks
-----------------

  040001b0: e3a01000        mov    r1, #0
  040001b4: e3a0300a        mov    r3, #10               ; settle counter
  040001b8: e3411201        movt   r1, #0x1201           ; r1 = CRG_START
  040001bc: e5912080        ldr    r2, [r1, #0x80]       ; r2 = CRG.SOC_CONFIG
  040001c0: e3c22e5f        bic    r2, r2, #0x5f0
  040001c4: e3c2200f        bic    r2, r2, #0xf          ; clear bits 0-10 except bit 9
  040001c8: e5812080        str    r2, [r1, #0x80]       ; no bits set back
  040001cc-040001d4:        nop / subs / bne     ; 10× nop settle
  040001d8: e12fff1e        bx     lr

  Mirror of configure_soc_clocks without the re-OR step.

main (bootloader flow at 0x04002704)
-------------------------------------

  04002704: e92d4010        push   {r4, lr}
  04002708: ebfffa86        bl     initial_memory_setup (0x4001128)
  0400270c: eb000aaa        bl     timer_start          (0x40051bc)
  04002710: eb000ab8        bl     timer_reset_counter  (0x40051f8)

  04002714: mov r3,#0 / movt r3,#0x1202 / ldr r2,[r3,#0x8c]
                                                        ; r2 = SYSCTRL.SYSSTAT
  04002720: e2124010        ands   r4, r2, #0x10        ; boot_alt = SYSSTAT & 0x10
  04002724: 1a00000a        bne    0x4002754             ; skip pinmux if set

  Default-boot pinmux block (boot_alt == 0):
    movt r3,#0x10ff                                     ; r3 = IO_CTRL0_START
    str  #0x6c1 [r3]            ; IOCFG1[0]
    str  #0x4f1 [r3, +4]        ; IOCFG1[1]
    str  #0x4f1 [r3, +8]        ; IOCFG1[2]
    str  #0x4f1 [r3, +12]       ; IOCFG1[3]
    str  #0x5f1 [r3, +16]       ; IOCFG1[4]
    str  #0x4f1 [r3, +20]       ; IOCFG1[5]

  04002754: mov r3,#0 / movt r3,#0x1202 / ldr r3,[r3,#0x30]
                                                        ; r3 = SYSCTRL.PERISTAT
  04002760: tst r3, #1
  04002764: bne 0x4002808                                ; path_a (PERISTAT.0 set)

  04002768: tst r2, #0x20                                ; SYSSTAT & 0x20 — fastboot hint
  0400276c: beq 0x40027a4                                ; skip fastboot if clear
  04002770: bl  fastboot_update (0x4002368)
  04002774: cmp r0, #0
  04002778: bne 0x40027a4                                ; on failure → is_update_mode

  return_success:    [0x400277c]
    04002778: (or fall-through)
    0400277c: bl  disable_instruction_cache (0x4002880)
    04002780-0400278c: r3 = *(u32*)0x04010188
    04002790: bne 0x4002798                              ; skip clock-reset if SRAM[0x188]!=0
    04002794: bl  reset_soc_clocks (0x40001b0)
    04002798: bl  timer_stop      (0x40051e8)
    0400279c: mov r0, #0
    040027a0: pop {r4, pc}                               ; return 0

  is_update_mode_check:  [0x40027a4]
    040027a4: bl  is_update_mode  (0x400105c)
    040027a8: cmp r0, #0
    040027ac: bne 0x4002834                              ; path_d if true
    040027b0: cmp r4, #0
    040027b4: bne 0x4002818                              ; path_c if boot_alt

  path_b (flash boot):   [0x40027b8]
    040027b8: bl  config_use_flash (0x400249c)
    040027bc: cmp r0, #0
    040027c0: beq 0x400277c                              ; success
    040027c4: bl  media_update    (0x40023c4)
    040027c8: cmp r0, #0
    040027cc: beq 0x400277c                              ; success
    (fall-through to erase_and_reset)

  erase_and_reset:    [0x40027d0]
    040027d0: r0 = SDRAM_START + 0x500
    040027d4: r2 = 0x6000
    040027d8: r1 = 0
    040027e0: bl  memset (0x4002900)                     ; zero 24KB at SDRAM+0x500
    040027e4: r0 = 500
    040027e8: bl  timer_wait_seconds (0x4002cb4)
    040027ec: bl  timer_stop          (0x40051e8)
    040027f0-040027fc: r3 = SYSCTRL_START / r2 = 1 / r0 = -1
    04002800: str r2, [r3, #4]                           ; SC_SYSRES = 1 (soft reset)
    04002804: pop {r4, pc}                               ; return -1 (unreachable)

  path_a:             [0x4002808]
    04002808: bl  bootrom_secure_check (0x4002284)
    0400280c: cmp r0, #0
    04002810: beq 0x400277c                              ; success
    04002814: b   0x40027d0                              ; erase_and_reset

  path_c:             [0x4002818]
    04002818: bl  bootrom_alt_media (0x40025d4)
    0400281c: cmp r0, #0
    04002820: beq 0x400277c                              ; success
    04002824: bl  media_update      (0x40023c4)
    04002828: cmp r0, #0
    0400282c: beq 0x400277c                              ; success
    04002830: b   0x40027d0                              ; erase_and_reset

  path_d:             [0x4002834]
    04002834: bl  media_update      (0x40023c4)
    04002838: cmp r0, #0
    0400283c: beq 0x400277c                              ; success
    04002840: b   0x40027b0                              ; re-test boot_alt → path_b/c

  Re-compiled freestanding bootloader.c preserves the full control-flow
  graph (success / erase_and_reset / paths a/b/c/d) but is not byte-
  identical — gcc reorders some blocks and uses slightly different
  instruction selection for the comparisons. Functionally equivalent
  modulo the stub return values.
