Showing results for 
Search instead for 
Did you mean: 
Valued Contributor III

Bootloader size



I'm writing a bootloader for NIOSII to load my application from an rs232 uart into SDRAM. 


What I'm trying to do is pretty much the same as described in this topic: ( 


I have a problem with the size of my bootloader. All the code I have written is: 


int main() 


int i; 

for (i=0; i<13;i++);  

return i; 



When I compile this, the linker output is as follows: 


Info: (boot_loader.elf) 4684 Bytes program size (code + initialized data). 


Why is my image so big? When I view the .elf file in the Nios II IDE, I see many symbols that I don&#39;t use, quite a few of them with an alt_ prefix. Since I never refer to them, I thought they would be stripped away at linking time, but it doesn&#39;t seem that way. 


Anybody got any hints/suggestions?
0 Kudos
8 Replies
Valued Contributor III

Additional question: Do I need to have separate SOPC settings for my bootloader and my application?

0 Kudos
Valued Contributor III

A bootloader like you describe would be very tricky (read: more trouble than it is worth) to make work. Why? Because you can&#39;t use the memory that you&#39;re trying to load your real program into for your bootloader at the same time; one will overwrite the other. Also, there&#39;s not any way to change the Nios2 Exception Address when you want to switch from the bootloader to the main program. 


For this reason, bootloaders generally get written in assembly. The source for the Altera boot-copier bootloader should be in your Nios install; I&#39;d recommend reading it and understanding it first before proceeeding, just so you&#39;re aware of everything a bootloader has to do. There are some restrictions on your bootloader:[list][*]You can&#39;t handle exceptions; the exception address is pointing at uninitialized memory. This means no interrupts; you&#39;ll need to manage I/O via polling, and make sure to never enable interrupts in your bootloader. The good news is that the bootloader doesn&#39;t really have much else to do. 

[*]You should try to use only the Nios CPU&#39;s register file for runtime variables. Anything you put in system RAM runs the risk of getting overwritten. 

[/list]Good luck!
0 Kudos
Valued Contributor III

Isn&#39;t it possible to do what they write about in the post I linked to in my first post? 


The plan is to have the bootloader in on board ram, and load the sw image to sdram. If you look at the post ( 


they describe exactly what I want to do. They give the following sopc settings: 


Reset address: on chip ram 

Exception address: user application memory (sdram) 




More awake now, so I&#39;ll express myself more clearly.  


I want to put the bootloder in on board ram, and let it load my image from uart to sdram. The memory where my bootloader lies will never be reused. My bootloader would like you said, poll the uart for data, place it in sdram, and finally point to the start address of the application when done. The exception address will be in sdram. The reset address will be in onboard memory, so that if the fpga is booted, it will run the bootloader again. 


And my problem is like stated above, that the size of my bootloader is too big. Almost all the code comes from my syslib, so I guess the question is how I reduce the size of this. Do I make a separate syslib for the application, that only "knows" about the uart and other things I may need?
0 Kudos
Valued Contributor III

for information: 


simple bootloader: 


you can write programs with the same quartus and SOPC settings and 

store these programs at different locations in flash. 

(Reset Address in flash at 0x00000000) 


I have a c_bootloader at add 0x00000000 in flash 

it can receive programs over uart (ymodem) and writes it into flash. 


e.g I have c_bootloader at add=0x00000000 in flash 

prog1 at add=0x00100000 in flash 

prog2 at add=0x00200000 in flash 


from the c_bootloader i can execute e.g. the following code to start  

prog2 from flash 


void (*pBoot)(void); // declare function pointer 

int startadd; // declare startaddress as integer 




pBoot(); // jump to new altera bootloader in flash 


the trick is, that every program in flash has the small altera_bootloader 

in front and copies itself from flash to ram and starts from ram. So the main  

(difficult) job is done from the altera_bootloader. 


the c_bootloader is just a noraml c-program, no assembler necessary. 

you can use the same settings of quartus and sopc builder for the  

c_bootloader and your other programs. 


see also topic  

How can I start different programs from flash 

in general discussion forum (
0 Kudos
Valued Contributor III

Thanks a lot for the good answer! Seems like this is the way to go for my application as well.

0 Kudos
Valued Contributor III

but why even small code, like 

int main(void) { for(;;); return 0; } 

is too big??? 

it should be less than 1 kbyte
0 Kudos
Valued Contributor III



I can reduce my bootloader to less than 100 Bytes. 


To implement this functionality, you should edit the file &#39;alt_sys_init.c&#39; which is created automatically by NiosII IDE. 


To solve this issue, you should be very familiar with NiosII software architecture, pls look for detailed information in Nios II Software Developer’s Handbook.pdf 



0 Kudos
Valued Contributor III

Assuming you&#39;re not using C++, edit alt_main.c so it just contains: 


void alt_main (void) {  alt_irq_init (ALT_IRQ_BASE);  main (alt_argc, alt_argv, alt_envp);  while (1);     } 


(file likely to be in directory C:\altera\kits\nios2_51\components\altera_hal\HAL\src


Comment out "call alt_load" in crt0.S (which might break static initialisation, or running code directly from flash, neither which I personally use) 


For both the project and the syslib, set the compiler optimisation option to "minimise size". 


In the system library properties, check "small C library" and "reduced device drivers" and set stdin/stdout/stderr to null. 


I also edit some of the "" files to prevent library code being sucked in. I also write my own serial I/O code (interrupt handlers). 


In window\preferences, Nios-II, check "generate objdump file"; this will allow you to see what&#39;s being included in the build. 


Hmmm it&#39;s about time I wrote this up (would Altera accept App-Note submissions?)
0 Kudos