Ethernet Bootloader 240630 221357
Ethernet Bootloader 240630 221357
Ethernet Bootloader
© 2003-2006 Andrew Smallridge
asmallri@brushelectronics.com
www.brushelectronics.com
Programmer
Application
UDP Ethernet
Operating System Packets
Socket API
Controlling Host
Bootloader
The Bootloader solution includes the bootloader code resident in the target PIC, the
controlling host (the host running the programming application) and an Ethernet
connection. For the sake of subsequent explanation, the term Bootloader refers to the
code executing in the LIA’s PIC microprocessor and the Programmer refers to the
programming application running on the controlling host.
• Plug and Play operation – the ability to use the bootloader without previously
having to configured it with an IP address
• No resources are required on the target PIC other than the flash memory
holding the boot code
Plug and Play operation over Ethernet is achieved via the use of multicast IP
operation enabling the dynamic discovery and operation of a LIA on a local network
segment. No “gleaming” is required to talk to the Bootloader. Remote support is
achieved via Unicast IP operation enabling a LIA on the other side of the world to be
remotely programmed. Flexible operating modes, embedded diagnostics and reliable
service challenge the third requirement, the need to minimise the memory footprint of
the loader to maximise the available user code space.
0x7000
Bootloader
Program
0x7FFF
MEMORY MAP
Basic Operation
The Bootloader code in the PIC and PC based programmer application use flags to
control the boot load process. Two of these flags determine the boot up behaviour of
the PIC:
• The PENDING RESET VECTOR is used to indicate that the user’s RESET
vector has not yet been received.
• The INVALID USER CODE SPACE flag indicates that an Intel End-of-File
hex record has not yet been received.
If neither of the critical flags has been set, the loader will wait for five seconds
looking for a command from the Programmer via the Ethernet interface to put the LIA
into LOADER COMMAND MODE. If no command is received the Bootloader
passes control to the user’s application code via the remapped reset vector. Note that
the Bootloader and the user’s code do not operate concurrently – this explains why the
loader does not require any of the PICs resources other than the consumed program
memory.
The Bootloader accepts commands from the Programmer using multicast and / or
unicast IP packets. UDP (User Datagram Protocol) is used between the Bootloader
and the programmer. The IP datagram service provides a good fit for exchanging Intel
Hex File records (lines in the hex file) however UDP is an unreliable service which
means that the application (both the Bootloader and the programmer) must implement
their own error detection, reporting and, where applicable, retransmission.
The LIA Response memo pane will usually be empty. This pane is used to display
error messages. The bootloader will use either multicast or unicast IP depending on
the settings contained in the configuration INI file located in the same directory as the
programming application.
Steps for downloading code to a LIA via the Bootloader and Programmer Application:
Step 1 – Power cycle or reset the target device to be programmed. This will
put the Bootloader into BOOTLOADER DISCOVERY MODE and enable the
Bootloader to respond to discovery packets from the programming application.
Step 2 – Click “Locate LIA” to initiate device discovery to find all devices
currently in BOOTLOADER DISCOVERY MODE. The first device to be
discovery will have it’s LIA_ID displayed in the Configure LIA drop down
control. If multiple devices were discovered then the target device can be
selected in the Configure LIA drop down box.
Step 4 – Select the desired read, erase or program operation. Note that to
program the Bootloader from a hex file, you must first select the source file
using the “File” button. The identified file and path will be displayed in the
file window. The hex file is opened when the Program button is clicked and
close at completion of the programming cycle. The next time the Program
button is clicked the source file is again opened. This is important because it
means that the Bootloader is always being programmed with the current
contents of the source file. The edit control Record to Program can be used for
sending individual records to the Bootloader. The Overwrite LIA button is
used to invalidate the user’s program memory space without erasing the user’s
program. A LIA in this mode will stay in BOOTLOADER DISCOVERY
MODE after being reset without passing control to the user’s program.
Step 5 – Once the LIA has been successfully programmed or read, click the
Reset LIA button which will issue a LOADER RESET COMMAND to the
Bootloader.
When the LOADER RESET COMMAND is executed the LIA executes the
Bootloader code and waits for 5 seconds to receive the LOADER COMMAND
MODE command. If this command is not received and the critical flags are clear then
the loader passes control to the user’s application program.
In the event that no status is returned for the current record, the timer will expire and a
consecutive error counter variable will be incremented. If this is CEC value below the
maximum consecutive error threshold then the timer is restarted and the previously
saved current record is retransmitted.
Packet Formats
Packets sent from the bootloader to the programmer can be sent to the Unicast
Address of the Bootloader or the Multicast Address. IP packets can be Unicast for
point to point communications, Broadcast to all devices on a subnet, or Multicast to a
targeted subset of devices. Devices interested in receiving multicast packets will
configure the network interfaces to accept specific multicast MAC layer addresses.
These MAC layer addresses map to corresponding IP multicast addresses. The
Bootloader configures the Ethernet Controller chip with a multicast filter mask in
order to accept multicast packets destined for the multicast address 230.10.10.11
Unicast packets sent by the Application Programmer will have the TTL (Time-to-Live)
field set to 255. Network routers along the path between the Source and Destination
decrement this value by 1. If as a result of decementing the TTL field, the result is
zero, then instead of forwarding the packet out of the destination interface the router
will discard the packet. This is the mechanism IP networks use to prevent packets
looping for ever in the network. For Unicast packets this means the packet can
traverse up to 254 routers.
For Multicast packets, the Programmer Application sets the TTL field to 1. The first
time a router tries to forward the packet, the TTL field is decremented to zero and the
router discards the packet. This mechanism limits Bootloader multicast packets to the
local IP network segment. Multicast therefore provides us a mechanism to support
plug and play operation. We do not need to assign an IP address to the Bootloader to
be able to communicate with it over an IP network and we use the TTL limited to
limit the traffic to the local segment. The multicast mechanism allows us to operate
multiple bootloaders on the same network segment. This is accomplished using the
LIA_ID which is derived from the MAC address of the Bootloader.
The Network Header portion is a convention UDP packet structure. The destination IP
address is either the Unicast IP Address of the LIA or the Multicast IP address
230.10.10.11 The destination UDP port number for the Bootloader is 0x4000.
The Programmer Command is carried in the data portion of the UDP packet. This
comprises a 7 byte command header and an optional variable length payload.
LIA_TAG: a fixed pattern used to protect against the remote possibility that another
device on the network is using the same IP address and port number. If the Key field
of the incoming packet does not match then the record is discarded. The bootloader
also checks to ensure all command structures are valid.
LIA_ID: the last two byes of the Bootloader’s MAC address. The Bootloader
generates a pseudo unique MAC address the very first time it is powered up. This
MAC address is subsequently stored in the Bootloader’s program memory space.
Subsequent powering up of the LIA will use the MAC address stored in the
Bootloader’s program memory space. This LIA ID mechanism enables multiple LIAs
to share a common network segment and still enable each LIA to be uniquely
programmed by the Programmer application. All Bootloaders will respond to a status
request if the target LIA_ID field is set to 0000. When the Bootloader responds it puts
it’s LIA_ID into the corresponding field. This is the mechanism used by the
Programmer Application to discover all Bootloaders on the network segment. Other
than the Status Command, the Bootloader will ignore all commands that do not
specifically match the Bootloader’s LIA_ID
The following diagram shows the Programmer Payload structure for packets send
from the programmer to the Bootloader. Each command comprises of a seven byte
command header.
The following figure gives a breakdown of the ten commands supported by the
bootloader and the length of the optional payload field.
Most commands are self explanatory and further information can be found in the
source code for the Programmer Application. The Invalidate command sets a flag in
the Bootloader code which prevents the bootloader from passing control to the user’s
program. Effectively the user’s program space has been invalidated but it has not been
erased. It is possible to reverse the Invalidate status by sending a single type 1 (end-
of-file) record to the Bootloader with the New_Code command.
The LDR_Mode command (Loader Mode) captures the Bootloader to prevent the
Bootloader passing control to the user’s application program at the end of the initial 5
second period from reset. In this mode the Bootloader is under the control of the
Programmer application. The user’s code is not invalidated – if the LIA is reset while
in this mode then after 5 seconds control will once again be passed to the user’s
program provided the user’s code space is valid.
The Dump command will upload the contents of the User’s program memory and
EEPROM memory space to the Programmer Application. The Bootloader uses the
same INHX32 Intel Hex record format.
All commands will result in a status packet being sent to the Programmer Application
indicating the success or otherwise of the command. Lack of status information is
interpreted by the programmer as a lost or missing command acknowledgement and
will result in the application programming reissuing the previous command. In the
worst case scenario this means the bootloader may program the same record (line of
the Hex file) twice.
The status record reflects the command status header information to the Programmer
Application as well as some additional fields. The Data sequence number is used for
lost packet detection for the dump command. The Application program currently uses
this information to report error status and terminate the dump command but it does
not automatically issue another dump command.
Where
: indicates the start of the current record
AA number of data bytes in the record
BBBB target address of the first byte in this record.
CC record Type
00 Data Record
01 End of File Record
04 Linear Address Record
DD.. data
EE checksum calculated across the record. Excludes the : character
Extract from INHX32 (Intel 32 bit hex file) produced by Microchip MPASM.
Record Type
:0400000080EF00F09D
:0400080080EF00F095
:10010000F29E0A0E86EC00F000EF00F0036EE80E9F
..
Configuration Bits Memory Space
:020000040030CA
:0E000000FFFAFEFEFFFFFBFFFFFFFFFFFFFF0B
:0200000400F00A EEPROM Memory Space
:100000003030204550524F4D204461746120737050
..
:00000001FF End of File
This figure show the use of type 4 records in the hex file which identifies the target
memory page used by the bootloader to determine the target for subsequent data (type
0) records. The Bootloader ignores data records targeted to the configuration bits. The
first record of the file is a type 4 record identifying the target for subsequent data
records in the Program Memory Space until the receipt of a subsequent type 4 record
or an EOF record.
Final Thoughts
The Ethernet Bootloader is a great tool for developing those “wouldn’t it be great to
reach this over the network” projects. Developing Ethernet enabled Microcontroller
projects is much easier today than when I started with the LIA. Microchip offers a
free IP stack for use with Microchip PICs and they have a number of good proof of
concept kits with the PICDEM.NET and the ENC28J60 Ethernet PigTail board. The
LIA Bootloader does not use any of the Microchip stack, this is because the
Bootloader is optimised for space, while the stack is optimised for performance. For
this reason I did not open any of the Bootloader APIs for use by target applications.
The contents of this note are an extract from the Ethernet Bootloader article that
appeared in the May 2004 issue of Circuit Cellar Magazine. The article an other
material relating to the article, such as image files for the PICDEM.NET board can be
found on the Circuit Cellar web site at:
http://www.circuitcellar.com/magazine/166toc.htm
Brush Electronics
2 Brush Court
Canning Vale
Western Australia 6155
Australia