What is a bootloader?
A bootloader is a piece of software, that is located in a special part of a microprocessors flash memory. It is able to communicate with the outside world via the pins of the processor (e.g. via RS232, SPI, I2C etc.). And it is able to change the content of the main part of the flash content.
What is it good for?
Typically a special device is needed to put new software into a microprocessor. A so called “programmer” is connected to the processor on one side, and to a PC on the other side. It transfers the new software from the PC to the processor. On the PC side there is a Windows application, that sends the information to the programmer. A bootloader makes it possible to connect the micro to the PC application directly (e.g. via RS232), read the new software from the PC and put it into the main part of the flash, the so called application section.
What is the main benefit of a boot loader?
Since the bootloader can be written in many different ways, it can be adapted to the needs of the specific application it is used in. The bootloader can even contain a decryption algorithm, so that new software can be given to the end-user for individual software upgrades, without the risk of reverse engineering or hacked firmware.
Who does need a bootloader?
A very common field of usage for a bootloader is portable consumer electronics like cell phones, personal digital organizers (PDAs), handheld GPS receivers, etc. New software updates are usually distributed via the Internet. It is in the manufacturers interest that the user can upload the software easily to the device without needing a special programmer hardware. But the user should not be able to disassemble or manipulate the software.
The procedure would be like this: The user downloads an encrypted software for his mobile phone from the Internet. He connects the phone to his PC via his standard RS232 cable. The bootloader in the telephone establishes a connection to the PC, reads the software, does the decryption and puts the software in application section of the phones flash memory.
What was the motivation for creating the bootloader that is published here?
This bootloader is being used at the amateur radio relay station in Nennslingen, Germany. It is part of the receiver, which does all the controlling within the relay station. Since there is a PC available with remote access at that location, there will be a permanent RS232 connection between the receiver and the PC. If the need for an software adaptation arises (e.g. due to interference problems), it can be done remotely, without the need to be on side for uploading the software. The used processor is the Atmel ATmega8, which supports the bootloader functionality.
What is the history behind this piece of software?
Atmel has published a bootloader written in C language in their application note AVR109. It is written using the IAR C compiler which is not free, so it is not available to everyone. The application note itself is well written and worth reading. It can be found at www.atmel.com.
On www.avrfreaks.net there is design note (#32) which describes a bootloader written in assembly for the ATmega163. It even has an counter implemented in the devices EEPROM to count the number of flash erases. However this software is not directly applicable to the most widely used AVR processor, the ATmega8. This software was used as a basis for the bootloader that is published here. The main advances are:
- check implemented to access the flash only after the last task was completed
- re-enabling the application section after it was reprogrammed by the bootloader
- interface sync-losses between AVRProg1.37 and the bootloader solved by implementing required commands that were missing
- startup code section that waits for 8 seconds for bootloader commands and starts the application if no commands are received (this makes it possible to use solely RXD and TXD lines on the RS232 for software upload, no further lines are required)
- Usage of “universal command” by AVRProg1.37 with regard to fuse/lock bit reading conquered to make things work
- Code is well documented now, every block and every line is exactly explained (this simplifies adaptations to individual requirements)
What do I have to know about he hardware configuration?
Well, not a lot. All you have to have is a RS232 level shifter (like a MAX232 or similar) that connects the UART input (RXD) and output (TXD) to your PC. You might want to connect a LED (with current limiting resistor, say 270Ohms) to I/O pin PB1 (active low, i.e. between Vcc and PB1). This LED shows you the state of the bootloader, as described below.
The AVRProg1.37 uses a fixed Baud rate of 19.2kB/s, this is achieved by using a 7.3728MHz crystal. Other crystals can be used but the UBR parameter has to be adapted in the source code in that case.
How can the bootloader be used?
Very simple. Use your traditional programmer to set the fuse bits for a 512 words (=1kByte) bootloader section size. Load the bootloader into the mega8 with a traditional programmer. It will automatically be placed within the last 1kByte of the flash. Set the BOOTRESET fuse bit using your programmer. This will cause the processor to start with the bootloader section from now on, rather than hitting the ground at address 0x00 after power-on.
From now on, you don’t need a programmer any more. After power-on, the bootloader will wait for an ESC (=ASCII 27) via the RS232 interface [the LED flashes]. If that ESC does come (e.g. sent from a terminal program running on the PC), the bootloader code will be activated [the LED stays on permanently] and the PC will be able to communicate to the processor as if there was a programmer connected. So simply start AVRProg1.37 and download your application software. Then close the AVRProg, start a terminal again, press ‘E’ and the bootloader software will terminate itself [LED will turn off] and start the application software.
If no ESC is received within 8 seconds after power on, the application program will start automatically. If there is no application software loaded, the bootloader will wait forever for an ESC.
Are there any open issues?
There are no known issues at this point in time. It should be noted that the write fuse bit button in the advanced window will not have any effect. AVRProg1.37 uses the ‘.’ command for setting the fuse bits. This is intentionally ignored to avoid the risk of locking oneself out. Also the regular commands for setting lock/fuse bits are intentionally ignored.
Download the bootloader version 2.2
- Source code of the bootloader version 2.2 for assembly in AVR-Studio 4
- Assembled bootloader in Intel Hex format, version 2.2
How does is look like when I use the bootloader?
Well, it looks just like if you were using an external programmer. The only difference is that the ATmega8 is the only device type that is offered to the user by AVRProg.
When you press the advance button, AVRProg shows you the state of the lock and fuse bits, the device signature, the identifier of the bootloader and the bootloader software revision. The calibration byte will always read 0x00, its readout is not supported by the bootloader.