Skip to content

Commit

Permalink
Command to get signature (commaai#399)
Browse files Browse the repository at this point in the history
* Commands to get signature from panda

* does this make misra happy?

* Can I do this in misra?

* this works and makes misra happy

* Add jenkins test for get signature

* Remove comments

* Add comment about code size

* Fix firmware file path
  • Loading branch information
pd0wm authored Dec 9, 2019
1 parent dad439a commit 7c13bec
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
20 changes: 20 additions & 0 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

#include "drivers/can.h"

extern int _app_start[0xc000]; // Only first 3 sectors of size 0x4000 are used

struct __attribute__((packed)) health_t {
uint32_t uptime_pkt;
uint32_t voltage_pkt;
Expand Down Expand Up @@ -364,6 +366,24 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, bool hardwired)
case 0xd2:
resp_len = get_health_pkt(resp);
break;
// **** 0xd3: get first 64 bytes of signature
case 0xd3:
{
resp_len = 64;
char * code = (char*)_app_start;
int code_len = _app_start[0];
(void)memcpy(resp, &code[code_len], resp_len);
}
break;
// **** 0xd4: get second 64 bytes of signature
case 0xd4:
{
resp_len = 64;
char * code = (char*)_app_start;
int code_len = _app_start[0];
(void)memcpy(resp, &code[code_len + 64], resp_len);
}
break;
// **** 0xd6: get version
case 0xd6:
COMPILE_TIME_ASSERT(sizeof(gitversion) <= MAX_RESP_LEN);
Expand Down
11 changes: 11 additions & 0 deletions python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,17 @@ def enter_bootloader(self):
def get_version(self):
return self._handle.controlRead(Panda.REQUEST_IN, 0xd6, 0, 0, 0x40).decode('utf8')

@staticmethod
def get_signature_from_firmware(fn):
f = open(fn, 'rb')
f.seek(-128, 2) # Seek from end of file
return f.read(128)

def get_signature(self):
part_1 = self._handle.controlRead(Panda.REQUEST_IN, 0xd3, 0, 0, 0x40)
part_2 = self._handle.controlRead(Panda.REQUEST_IN, 0xd4, 0, 0, 0x40)
return part_1 + part_2

def get_type(self):
return self._handle.controlRead(Panda.REQUEST_IN, 0xc1, 0, 0, 0x40)

Expand Down
19 changes: 19 additions & 0 deletions tests/automated/1_program.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import os

from nose.tools import assert_equal

from panda import Panda, BASEDIR
from .helpers import reset_pandas, test_all_pandas, panda_connect_and_init


# Reset the pandas before flashing them
def aaaa_reset_before_tests():
reset_pandas()


@test_all_pandas
@panda_connect_and_init
def test_recover(p):
assert p.recover(timeout=30)


@test_all_pandas
@panda_connect_and_init
def test_flash(p):
p.flash()


@test_all_pandas
@panda_connect_and_init
def test_get_signature(p):
fn = os.path.join(BASEDIR, "board/obj/panda.bin")

firmware_sig = Panda.get_signature_from_firmware(fn)
panda_sig = p.get_signature()

assert_equal(panda_sig, firmware_sig)

0 comments on commit 7c13bec

Please sign in to comment.