Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bootloader: Add Reading of Git SHA to bootloader #28492

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Tools/AP_Bootloader/AP_Bootloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#include <AP_CheckFirmware/AP_CheckFirmware.h>
#include "network.h"

#define FORCE_VERSION_H_INCLUDE
#include "ap_version.h"
#undef FORCE_VERSION_H_INCLUDE

extern "C" {
int main(void);
}
Expand All @@ -47,7 +51,8 @@ struct boardinfo board_info = {
.board_type = APJ_BOARD_ID,
.board_rev = 0,
.fw_size = (BOARD_FLASH_SIZE - (FLASH_BOOTLOADER_LOAD_KB + FLASH_RESERVE_END_KB + APP_START_OFFSET_KB))*1024,
.extf_size = (EXT_FLASH_SIZE_MB * 1024 * 1024) - (EXT_FLASH_RESERVE_START_KB + EXT_FLASH_RESERVE_END_KB) * 1024
.extf_size = (EXT_FLASH_SIZE_MB * 1024 * 1024) - (EXT_FLASH_RESERVE_START_KB + EXT_FLASH_RESERVE_END_KB) * 1024,
.git_hash = GIT_VERSION_INT
};

#ifndef HAL_BOOTLOADER_TIMEOUT
Expand Down
36 changes: 27 additions & 9 deletions Tools/AP_Bootloader/bl_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
// RESET finalise flash programming, reset chip and starts application
//

#define BL_PROTOCOL_VERSION 5 // The revision of the bootloader protocol
#define BL_PROTOCOL_VERSION 6 // The revision of the bootloader protocol
// protocol bytes
#define PROTO_INSYNC 0x12 // 'in sync' byte sent before status
#define PROTO_EOC 0x20 // end of command
Expand Down Expand Up @@ -122,14 +122,28 @@
#define PROTO_READ_MULTI_MAX 255 // size of the size field

/* argument values for PROTO_GET_DEVICE */
#define PROTO_DEVICE_BL_REV 1 // bootloader revision
#define PROTO_DEVICE_BOARD_ID 2 // board ID
#define PROTO_DEVICE_BOARD_REV 3 // board revision
#define PROTO_DEVICE_FW_SIZE 4 // size of flashable area
#define PROTO_DEVICE_VEC_AREA 5 // contents of reserved vectors 7-10
#define PROTO_DEVICE_EXTF_SIZE 6 // size of available external flash
#define PROTO_DEVICE_BL_REV 1 // bootloader revision
#define PROTO_DEVICE_BOARD_ID 2 // board ID
#define PROTO_DEVICE_BOARD_REV 3 // board revision
#define PROTO_DEVICE_FW_SIZE 4 // size of flashable area
#define PROTO_DEVICE_VEC_AREA 5 // contents of reserved vectors 7-10
#define PROTO_DEVICE_EXTF_SIZE 6 // size of available external flash
#define PROTO_DEVICE_BL_GIT_HASH 7 // git hash of bootloader build (requires > v5)

// all except PROTO_DEVICE_VEC_AREA and PROTO_DEVICE_BOARD_REV should be done
#define CHECK_GET_DEVICE_FINISHED(x) ((x & (0xB)) == 0xB)

// Convert the Device Info to a bitmask
#define DEVICE_INFO_MASK(x) (1<<(x-1))

// Define the required device info as a mask
#define REQUIRED_DEVICE_INFO_MASK ( \
DEVICE_INFO_MASK(PROTO_DEVICE_BL_REV) \
| DEVICE_INFO_MASK(PROTO_DEVICE_BOARD_ID) \
| DEVICE_INFO_MASK(PROTO_DEVICE_FW_SIZE) \
)

// Checks whether all required device info has been obtained
#define CHECK_GET_DEVICE_FINISHED(x) ((x & (REQUIRED_DEVICE_INFO_MASK)) == REQUIRED_DEVICE_INFO_MASK)

// interrupt vector table for STM32
#define SCB_VTOR 0xE000ED08
Expand Down Expand Up @@ -602,11 +616,15 @@ bootloader(unsigned timeout)
case PROTO_DEVICE_EXTF_SIZE:
cout((uint8_t *)&board_info.extf_size, sizeof(board_info.extf_size));
break;

case PROTO_DEVICE_BL_GIT_HASH:
cout((uint8_t *)&board_info.git_hash, sizeof(board_info.git_hash));
break;

default:
goto cmd_bad;
}
done_get_device_flags |= (1<<(arg-1)); // set the flags for use when resetting timeout
done_get_device_flags |= DEVICE_INFO_MASK(arg); // set the flags for use when resetting timeout
break;

// erase and prepare for programming
Expand Down
1 change: 1 addition & 0 deletions Tools/AP_Bootloader/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct boardinfo {
uint32_t board_rev;
uint32_t fw_size;
uint32_t extf_size;
uint32_t git_hash;
} __attribute__((packed));

extern struct boardinfo board_info;
Expand Down
18 changes: 18 additions & 0 deletions Tools/AP_Bootloader/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def build(bld):
else:
flashiface_lib = []

_build_dynamic_sources(bld)

bld.ap_stlib(
name= 'AP_Bootloader_libs',
use='dronecan',
Expand All @@ -34,3 +36,19 @@ def build(bld):
use=['AP_Bootloader_libs', 'libcanard', 'dronecan'],
program_groups='bootloader'
)

def _build_dynamic_sources(bld):
def write_version_header(tsk):
bld = tsk.generator.bld
return bld.write_version_header(tsk.outputs[0].abspath())

bld(
name='ap_version',
target='ap_version.h',
vars=['AP_VERSION_ITEMS'],
rule=write_version_header,
)

bld.env.prepend_value('INCLUDES', [
bld.bldnode.abspath(),
])
22 changes: 15 additions & 7 deletions Tools/scripts/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,14 @@ class uploader(object):

CHIP_FULL_ERASE = b'\x40' # full erase of flash

INFO_BL_REV = b'\x01' # bootloader protocol revision
BL_REV_MIN = 2 # minimum supported bootloader protocol
BL_REV_MAX = 5 # maximum supported bootloader protocol
INFO_BOARD_ID = b'\x02' # board type
INFO_BOARD_REV = b'\x03' # board revision
INFO_FLASH_SIZE = b'\x04' # max firmware size in bytes
INFO_EXTF_SIZE = b'\x06' # available external flash size
INFO_BL_REV = b'\x01' # bootloader protocol revision
BL_REV_MIN = 2 # minimum supported bootloader protocol
BL_REV_MAX = 6 # maximum supported bootloader protocol
INFO_BOARD_ID = b'\x02' # board type
INFO_BOARD_REV = b'\x03' # board revision
INFO_FLASH_SIZE = b'\x04' # max firmware size in bytes
INFO_EXTF_SIZE = b'\x06' # available external flash size
INFO_BL_GIT_HASH = b'\x07' # git hash of bootloader build

PROG_MULTI_MAX = 252 # protocol max is 255, must be multiple of 4
READ_MULTI_MAX = 252 # protocol max is 255
Expand Down Expand Up @@ -737,6 +738,10 @@ def identify(self):
self.board_rev = self.__getInfo(uploader.INFO_BOARD_REV)
self.fw_maxsize = self.__getInfo(uploader.INFO_FLASH_SIZE)

# Git SHA introduced added in v6
if self.bl_rev > 5:
self.git_hash_bl = self.__getInfo(uploader.INFO_BL_GIT_HASH)

def dump_board_info(self):
# OTP added in v4:
print("Bootloader Protocol: %u" % self.bl_rev)
Expand Down Expand Up @@ -839,6 +844,9 @@ def dump_board_info(self):
print(" board_type: %u" % self.board_type)
print(" board_rev: %u" % self.board_rev)

git_hash_bl = "%x" % (self.git_hash_bl) if self.bl_rev > 5 else "UNKNOWN"
print(" git hash (Bootloader): %s" % git_hash_bl)

print("Identification complete")

def board_name_for_board_id(self, board_id):
Expand Down
Loading