PDA

View Full Version : recommendations for secure bootloaders



longpole001
- 14th August 2014, 07:59
Hi Guys ,

i am looking for a secure bootloader code either prefer free but will look at ones that require a license purchase , to be incorporated into my project for 18F chips , so users can update via the net via the usb / serial

Can someone with experience with it being used in PBP and the code overhead issues if any that need to be considered

please advise


Regards

Sheldon

pedja089
- 14th August 2014, 09:46
I just finish my first bootloader and it use simple data encription. I isn't so hard to make one. I put bootloader on end of program memory(DEFINE RESET_ORG), so main app isn't affected with bootloader. If you want to jump to bootloader just execute @ GOTO 1EC00h in main app.
Code for bootloader is simple. Here is part of code for PIC:


DEFINE RESET_ORG 1EC00h
'Var definition and
'Set registers
BlockSize VAR BYTE BANKA SYSTEM
'Get BLOCK SIZE from ASM
@ MovLW BLOCK_SIZE
@ MovWF BlockSize
MemSize=79999' Used first 80000 bytes of FLASH
PAUSE 6
FOR Adr=0 TO MemSize STEP BlockSize
ERASECODE Adr
NEXT Adr
FOR Adr=0 TO MemSize STEP 128
Err=0
FwReceive:
Err=Err+1
IF Err=5THEN 'If PC doesn't respond, do something...

ENDIF
HSEROUT2 ["A:",DEC6 Adr,13,10] 'Send address to PC
HSERIN2 100, FwReceive, [WAIT("D:"), Tmp0, Tmp1] 'Receive same byte twice, to eliminate communication errors..
IF Tmp0<>Tmp1 THEN GOTO FwReceive
GOSUB ByteDecode
WRITE Adr, Tmp0 'Write will first fill up table then it will write 64 or 128 bytes to memory. depending on BlockSize, Check PBP manual
NEXT Adr
HSEROUT2 ["C"]
@ RESET

You can add EEPROM, yust using READ and WRITE
Or you can add config bytes, thanks to Darrel Taylor.
http://www.picbasic.co.uk/forum/showthread.php?t=4093
For PC side you can use http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en012031 as starting point.
I menage to translate this app to VB .net, get FLASH, EEPROM and CONFIG data from hex file. From that point it was easy to add simple byte encryption and communication.
Communication code in Vb.net

Private Sub tmrWriteToDevice_Tick(sender As Object, e As EventArgs) Handles tmrWriteToDevice.Tick
Static RxBuffer As String
If SerialPort1.IsOpen = True Then
RxBuffer += SerialPort1.ReadExisting
If RxBuffer.Length > 0 Then
If InStr(RxBuffer, "A:", CompareMethod.Text) > 0 And Mid(RxBuffer, Len(RxBuffer), 1) = Chr(3) Then
Dim Adr As Integer, DataPos As Integer
DataPos = InStr(RxBuffer, "A:")
RxBuffer = Mid(RxBuffer, DataPos + 2, 6) 'Adress string
Adr = Convert.ToInt32(RxBuffer, 10)
RxBuffer = ""
ByteArray(0) = HexFileByte(Adr) 'HexFileByte if loaded with flesh data from hex, and encoded.
ByteArray(1) = HexFileByte(Adr)
SerialPort1.Write("D:")
SerialPort1.Write(ByteArray, 0, 2)
ToolStripProgressBar1.Value = Adr / 800
ToolStripStatusLabel1.Text = ToolStripProgressBar1.Value.ToString & "% Complited"

ElseIf InStr(RxBuffer, "C" & Chr(3), CompareMethod.Text) > 0 Then
ToolStripStatusLabel1.Text = "Data Transfer Complite"
ToolStripProgressBar1.Value = 100
End If
End If
End If
End Sub
I tried to put bootloader in main app, but that was very hard. Only way that it could be done is to compile your bootloader, then get flash memory from hex, remove part before and after bootloader, then use ORG 1EC00h and dw(thanks to DT http://www.picbasic.co.uk/forum/showthread.php?t=3891#LAB) to put data to flash directly.
From this I created another one to boot from I2C memory, so in bootloader there is only I2C communication so it is only about 1K. Another advantige is that you can load I2C memory from any communication that you have on your application(eg serial port, bluetooth, usb etc..) and still be able to use same bootloader. Idea taken from Mike
https://www.youtube.com/watch?v=jbLy6kE-Szg&list=UUcs0ZkP_as4PpHDhFcmCHyA
I hope this will be enough to create your bootloader.

longpole001
- 14th August 2014, 10:08
thanks , there is bit there to understand ,

I have some of the same issues in that i need to load code that writes to an external flash chip the data required , clears that code then loads the operational code. would also like to make it robust in that if the load fails it restores the orginal , is this something you allowed for as well ?

pedja089
- 14th August 2014, 10:43
With I2C bootloader you have that protection. If data in external I2C memory isn't complete or if it is corrupted then main app won't start bootloader, simple as that... Erasing and writing to flash isn't started until you have all data in external memory.
Also another advantage of this is approach is that interrupt vector isn't affected by bootloader. And PIC is able to erase and write memory with code protect turned on.
Only possible drawback of this is that bootloader isn't run before main app. So if you have bug's in your main app, there is possibility that you won't be able to start bootloader.
But I didn't need that, I just needed that user can load another firmware version.

longpole001
- 14th August 2014, 22:47
On the PC side - i have not worked on vb for ages , but i need a nice GUI for the interface , have you seen / done any that would suit this

pedja089
- 15th August 2014, 00:13
No, that is reason why I converted to VB.net.

EarlyBird2
- 15th August 2014, 08:03
This is latest free MS offering

http://www.microsoft.com/en-gb/download/details.aspx?id=40787

Need help with it? Just ask as usual.

longpole001
- 16th August 2014, 05:05
looks interesting

Heckler
- 22nd August 2014, 13:52
Here is a podcast related to bootloaders, some really good information there...

http://traffic.libsyn.com/makingembeddedsystems/mes-ep34.mp3

There are several other very interesting topics there on Embedded.fm

longpole001
- 29th August 2014, 00:31
thanks guys , it part of the project i need to do , just not had time to to do it , examples are good ,like to buy the finished code , just to save time to do this but in the end i am sure ill have to write it

like most projects using flash i need to have hex just for the programming of the flash chip , simply cos the code that generates the data for the flash is too big to fit into the cpu as well as the main running code
then the actual code.

i am also looking at spiting up functions over 2 cpus for other reasons , so making the updates a bit more interesting