-2

This is part of the init code of a downloaded firmware of some stm32f030 microcontroller. It was disassembled using radare2. It is called after the clock has been initialized but before the heap is.

0x0800335c      00f00bf8       bl fcn.08003376
0x08003360      0028           cmp r0, 0       
0x08003362      01d0           beq 0x8003368   
0x08003364      fff7d4ff       bl INIT2        
0x08003368      0020           movs r0, 0
[...]
0x08003376      0120           movs r0, 1      
0x08003378      7047           bx lr           

As far as I can tell r0 gets always set to 1 so INIT2 is never skipped. I don't get the point. What am I missing?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Scheintod
  • 7,953
  • 9
  • 42
  • 61
  • 2
    Maybe this firmware is used for multiple devices. Some have 1, some have 0. Apparently the compiler could not optimize it out. – Jester Feb 13 '23 at 19:28
  • 3
    Without LTO, a function in another unit won't get inlined. If there is another source file with a function DeviceSupportsFeatureX, then it will appear like that. – ElderBug Feb 13 '23 at 19:43
  • Great idea. But I think INIT2 is part of whatever compiler this wrote startup code and does heap initialization. It's not particularily easy to tell because it seems to use some kind of memory table generated jump table stuff ... so then this is an "is feature enabled" flag it would be some "do we use a c compiler flag". would be more ease if I knew what compiler made that. – Scheintod Feb 13 '23 at 20:11
  • 1
    It could be code that recognizes whether it is executing from RAM or ROM. 'INIT2' will not be needed if it is running from RAM. Probably you always run from ROM? – artless noise Feb 18 '23 at 18:46

1 Answers1

3

I agree with ElderBug's interpretation in the comments that the first block of code is from a unit that supports multiple builds, and the second function is from a unit that has been configured for a particular build.

Unless you edit the question to provide more detail, we can only guess at the purpose, but here is one plausible interpretation that is similar to code I have worked on:

You want to skip heap initialization in INIT2 on a warm-boot, where RAM has been retained through a low-power mode. The particular binary you have doesn't support low-power modes so its function is hard-coded to return non-zero, but in another build it might read the power registers and can return zero if the RAM was retained.

Tom V
  • 4,827
  • 2
  • 5
  • 22