Skip to content

Commit

Permalink
Merge pull request #382 from PW-Sat2/safe-mode-crc
Browse files Browse the repository at this point in the history
Safe mode & bootloader padding
  • Loading branch information
zaq32 authored Nov 21, 2017
2 parents 21b9d7f + d692397 commit 7156ad0
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def buildPlatform(mcu, pld) {
ENABLE_LTO: 1,
COMM_SECURITY_CODE: env.SECURITY_CODE
])
bat "make pwsat boot safe_mode pwsat.hex pwsat.bin boot.hex safe_mode.bin generate_telemetry generate_exp_data generate_persistent_state"
bat "make pwsat boot safe_mode pwsat.hex pwsat.bin boot.bin safe_mode.bin generate_telemetry generate_exp_data generate_persistent_state"
}
}

Expand Down Expand Up @@ -173,4 +173,4 @@ node('pwsat-build') {
// }
// }
// }
// }
// }
3 changes: 1 addition & 2 deletions boot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ target_link_libraries(${NAME}

target_include_directories(${NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/gen)

target_generate_hex(${NAME})
target_generate_bin(${NAME})
target_generate_padded_bin(${NAME} 65536 0xFF)

target_memory_report(${NAME})

Expand Down
3 changes: 3 additions & 0 deletions integration_tests/tools/crc.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def read_content(path):


def pad(content, multiply_of, pad_with):
if len(content) % multiply_of == 0:
return content

pad_length = multiply_of - len(content) % multiply_of

return content + bytearray([pad_with] * pad_length)
Expand Down
33 changes: 33 additions & 0 deletions integration_tests/tools/pad_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import argparse

import sys

parser = argparse.ArgumentParser()


def int_dec_or_hex(v):
if v[0:2] == '0x':
return int(v, 16)

return int(v)


parser.add_argument("file", help="File that will be padded")
parser.add_argument("size", help="Size of the file with padding", type=int_dec_or_hex)
parser.add_argument("pad", help="Byte used as padding", type=int_dec_or_hex, default=0)

args = parser.parse_args()

with file(args.file, 'r+b') as f:
f.seek(0, 2)

pos = f.tell()

if pos > args.size:
sys.stderr.write("Unable to pad to size {} bytes as file is already bigger ({} bytes)".format(pos, args.size))
exit(-1)

padding_size = args.size - pos
padding = chr(args.pad) * padding_size

f.write(padding)
2 changes: 1 addition & 1 deletion jlink/flash.jlink.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ si 1
speed auto
r
halt
loadbin "${HEX_FILE}", ${BASE_ADDRESS}
loadbin "${FLASH_FILE}", ${BASE_ADDRESS}
r
g
exit
2 changes: 1 addition & 1 deletion safe_mode/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ target_link_libraries(${NAME}

target_include_directories(${NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/gen)

target_generate_bin(${NAME})
target_generate_padded_bin(${NAME} 32768 0x00)

target_memory_report(${NAME})

Expand Down
40 changes: 31 additions & 9 deletions utils/functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,33 @@ function(target_generate_bin TARGET)
add_custom_target (${TARGET}.bin DEPENDS ${BIN_OBJ})
endfunction(target_generate_bin)

function(target_generate_padded_bin TARGET SIZE PADDING)
set (EXEC_OBJ ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET})
set (BIN_OBJ ${EXEC_OBJ}.bin)

set_target_properties(${TARGET} PROPERTIES BIN_FILE ${BIN_OBJ})

add_custom_command(OUTPUT ${BIN_OBJ}
COMMAND ${CMAKE_OBJCOPY} -R .boot_params -O binary ${EXEC_OBJ} ${BIN_OBJ}
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/integration_tests/tools/pad_file.py ${BIN_OBJ} ${SIZE} ${PADDING}
DEPENDS ${TARGET}
)

add_custom_target (${TARGET}.bin DEPENDS ${BIN_OBJ})
endfunction(target_generate_padded_bin)

function(target_jlink_flash TARGET BASE_ADDRESS)
set(COMMAND_FILE ${CMAKE_BINARY_DIR}/jlink/${TARGET}.flash.jlink)

get_property(HEX_FILE TARGET ${TARGET} PROPERTY HEX_FILE)
get_property(FLASH_FILE TARGET ${TARGET} PROPERTY HEX_FILE)

if("${FLASH_FILE}" STREQUAL "")
get_property(FLASH_FILE TARGET ${TARGET} PROPERTY BIN_FILE)
endif()

configure_file(${CMAKE_SOURCE_DIR}/jlink/flash.jlink.template ${COMMAND_FILE})

unset(HEX_FILE)
unset(FLASH_FILE)

set(JLINK_ARGS
"-device" ${DEVICE}
Expand All @@ -48,7 +66,7 @@ function(target_jlink_flash TARGET BASE_ADDRESS)

add_custom_target(${TARGET}.flash
COMMAND ${JLINK} ${JLINK_ARGS}
DEPENDS ${TARGET}.hex
DEPENDS ${FLASH_FILE}
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)

Expand Down Expand Up @@ -97,12 +115,16 @@ function(generate_version_file FILENAME)
OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(GIT_BRANCH $ENV{BRANCH_NAME})

if("${GIT_BRANCH}" STREQUAL "")
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()

cmake_host_system_information(RESULT HOST_NAME QUERY HOSTNAME)

Expand Down

0 comments on commit 7156ad0

Please sign in to comment.