{"id":21070715,"url":"https://github.com/microbit-foundation/v2-bootloader","last_synced_at":"2025-05-16T05:31:02.310Z","repository":{"id":49161682,"uuid":"244635890","full_name":"microbit-foundation/v2-bootloader","owner":"microbit-foundation","description":"The source code of the bootloader used by the nRF52 on the micro:bit v2","archived":false,"fork":false,"pushed_at":"2022-07-14T10:01:26.000Z","size":266,"stargazers_count":5,"open_issues_count":2,"forks_count":7,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-03T20:37:35.013Z","etag":null,"topics":["microbit"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/microbit-foundation.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-03T12:55:05.000Z","updated_at":"2024-10-24T12:46:01.000Z","dependencies_parsed_at":"2022-09-04T11:51:43.900Z","dependency_job_id":null,"html_url":"https://github.com/microbit-foundation/v2-bootloader","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microbit-foundation%2Fv2-bootloader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microbit-foundation%2Fv2-bootloader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microbit-foundation%2Fv2-bootloader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microbit-foundation%2Fv2-bootloader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/microbit-foundation","download_url":"https://codeload.github.com/microbit-foundation/v2-bootloader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254473853,"owners_count":22077187,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["microbit"],"created_at":"2024-11-19T18:47:55.009Z","updated_at":"2025-05-16T05:30:57.299Z","avatar_url":"https://github.com/microbit-foundation.png","language":"C","readme":"# micro:bit v2 bootloader\r\n\r\nThis repository contains the code required to compile the bootloader for micro:bit V2. This description is taken from the Nordic infocenter:\r\n\r\n\r\n```\r\n\r\nBootloader\r\n\r\nThe bootloader module is responsible for:\r\n\r\n    booting into an application,\r\n    activating new firmware,\r\n    optionally, entering DFU mode where DFU transports are activated and new firmware can be delivered,\r\n    feeding the watchdog timer.\r\n\r\nEach bootloader example provided in this SDK contains one DFU transport.\r\n\r\n```\r\n\r\nThe source of the bootloader can be found in `./bootloader`, a simple SDK to interact with the micro:bit's peripherals can be found in `./microbit_sdk`, and modifications to the nRF5SDK can be found in `./nRF5SDK_mods`.\r\n\r\nThe mods are required to add a custom init packet, skip write protection, and display DFU progress on the LEDs.\r\n\r\n# Building this repo\r\n\r\nThere should be these folders here: \u003cbr /\u003e\r\n\r\nnRF5SDK - Nordic SDK v16 when downloaded \u003cbr /\u003e\r\nbootloader - from examples/dfu/secure_bootloader \u003cbr /\u003e\r\ntestapp - from examples/ble_peripheral/ble_app_buttonless_dfu \u003cbr /\u003e\r\n\r\n\r\n### getting started \u003cbr /\u003e\r\nClone this repo and download nRF5SDK as below. \u003cbr /\u003e\r\n\r\n@martinwork has been using and updating the SES (Segger Embedded Studio) projects \u003cbr /\u003e\r\nSES latest version is 4.30a but SDK 16 release notes mention v4.18. \u003cbr /\u003e\r\nSES uses .emProject files to store project information. These are XML files listing information such as source files, directories, and compiler flags. \u003cbr /\u003e\r\nhttps://infocenter.nordicsemi.com/topic/struct_nrf5gs/struct/nrf5gs_sw_dev.html \u003cbr /\u003e\r\n\r\n@microbit-sam has been using and updating the Makefile based projects. \u003cbr /\u003e\r\n\r\nBoth approaches to building use bash scripts, [nrfutil](https://github.com/NordicSemiconductor/pc-nrfutil), and the [Arm embedded toolchain](https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm) to build the bootloader and related program files.\r\n\r\n### nRF5SDK \u003cbr /\u003e\r\nDownload the SDK folder zip (tested 16.0.0) from Nordic, or from the tagged release on this [repository](https://github.com/microbit-foundation/v2-bootloader/releases/tag/sdk16)\u003cbr /\u003e\r\nhttps://www.nordicsemi.com/Software-and-tools/Software/nRF5-SDK/Download \u003cbr /\u003e\r\nUnzip and rename to nRF5SDK \u003cbr /\u003e\r\nDownload and build micro-ecc as described here: \u003cbr /\u003e\r\nhttps://infocenter.nordicsemi.com/topic/sdk_nrf5_v16.0.0/lib_crypto_backend_micro_ecc.html \u003cbr /\u003e\r\nI installed the ARM GCC toolchain at the location specified in\r\nnRF5SDK⁩/components⁩/toolchain⁩gcc/Makefile.posix \u003cbr /\u003e\r\nand built nRF5SDK⁩/external⁩/micro-ecc/nrf52hf_armgcc/armgcc/Makefile \u003cbr /\u003e\r\n\r\n\r\n### Testing DFU \u003cbr /\u003e\r\nWith SES, load the emProject in bootloader/microbit_debug/ses. \u003cbr /\u003e\r\nChoose Build/Build and Run - this will download SoftDevice and the bootloader. \u003cbr /\u003e\r\nLoad the emProject in testapp/microbit/ses. \u003cbr /\u003e\r\nSelect the Debug configuration and choose Build/Build Solution. \u003cbr /\u003e\r\n\r\nAlternatively, build the test app using make \r\n`make -C ./testapp/microbit/armgcc`\r\n\r\nUsing \"scripts/package_testapp_debug\" [see \"scripts/testapp_package\" below], generate \"testapp_debug.zip\" . \u003cbr /\u003e\r\nCopy the zip to an Android device or to e.g. Google Drive. \u003cbr /\u003e\r\n\r\nWith Android nRF Connect, download testapp.zip: \u003cbr /\u003e\r\n    Refresh the scan to find DfuTarg and connect to it. \u003cbr /\u003e\r\n    Tap DFU at top right, choose \"Distribution packet (ZIP)\" and locate the zip. \u003cbr /\u003e\r\n\r\nReturn to SCANNER, scan to find \"Nordic_buttonless\" and connect to it. \u003cbr /\u003e\r\n    Tap Secure DFU Service. \u003cbr /\u003e\r\n    Tap the up/down arrows to enable notifications. \u003cbr /\u003e\r\n    Tap the up arrow and tap SEND in the alert. \u003cbr /\u003e\r\n\r\nReturn to SCANNER, scan to find \"DfuTarg\". \u003cbr /\u003e\r\n    Reset microbit and check \"Nordic_buttonless\" reappears. \u003cbr /\u003e\r\n\r\nWith SES, choose Debug/Go to debug the app. \u003cbr /\u003e\r\nSwitch to the microbit_bootloader_debug project and choose Debug/Go to debug the bootloader.\r\n\r\n\r\n### Making a complete hex \u003cbr /\u003e\r\nSee \"scripts/merge_testapp and merge_testapp_debug\" below. \u003cbr /\u003e\r\nThe bootloader won't launch the application without a settings page. \u003cbr /\u003e\r\nhttps://infocenter.nordicsemi.com/topic/sdk_nrf5_v16.0.0/lib_bootloader.html. \u003cbr /\u003e\r\nhttps://infocenter.nordicsemi.com/topic/ug_nrfutil/UG/nrfutil/nrfutil_settings_generate_display.html \u003cbr /\u003e\r\n\r\n\r\n### Bonding \u003cbr /\u003e\r\nThe bootloader can require bonds by setting NRF_DFU_BLE_REQUIRES_BONDS and NRF_SDH_BLE_SERVICE_CHANGED. \u003cbr /\u003e\r\nIn this case, if the bootloader does not find valid peer data (see nrf_dfu_settings_peer_data_is_valid), it will simply reset. \u003cbr /\u003e\r\nThe peer data can only be provided by a valid application which has done the bonding (with NRF_DFU_BLE_BUTTONLESS_SUPPORTS_BONDS and NRF_SDH_BLE_SERVICE_CHANGED set). \u003cbr /\u003e\r\n\r\n\r\n### testapp placement macros \u003cbr /\u003e\r\nThese are modified to add calculations. \u003cbr /\u003e\r\nFLASH_SIZE=(0x72000-FLASH_START), so it will work with both release and debug bootloaders. \u003cbr /\u003e\r\nThe bootloaders have FLASH_START 0x72000 (debug) and 0x78000 (release). \u003cbr /\u003e\r\nFLASH_START=0x27000 is correct for S140 7.0.1. \u003cbr /\u003e\r\nRAM_START=0x20002270 is the value calculated by sd_ble_enable. \u003cbr /\u003e\r\nhttps://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s140.api.v7.0.1/group___b_l_e___c_o_m_m_o_n___f_u_n_c_t_i_o_n_s.html \u003cbr /\u003e\r\nSee Memory layout here: \u003cbr /\u003e\r\nhttps://infocenter.nordicsemi.com/topic/sdk_nrf5_v16.0.0/lib_bootloader.html \u003cbr /\u003e\r\nThe application start address is given as 0x0002 6000, which seems to be wrong. \u003cbr /\u003e\r\n \r\n7F000 bootloader_settings_page \u003cbr /\u003e\r\n7E000 mbr_params_page / bootloader settings backup \u003cbr /\u003e\r\n78000 Bootloader (72000 for debug bootloader) \u003cbr /\u003e\r\n27000 Application \u003cbr /\u003e\r\n01000 SoftDevice \u003cbr /\u003e\r\n00000 MBR \u003cbr /\u003e\r\n\r\n\r\n### scripts/make_dfu_keys \u003cbr /\u003e\r\nThis is a bash script. Double click it or copy the nrfutil command out of it. \u003cbr /\u003e\r\nUses \"nrfutil keys\" to generate private and public keys in its folder. \u003cbr /\u003e\r\nThe public key needs to be copied to the bootloader source. \u003cbr /\u003e\r\nThe private key is used create a signature for each application hex to be checked by the bootloader. \u003cbr /\u003e\r\nhttps://infocenter.nordicsemi.com/topic/sdk_nrf5_v16.0.0/lib_bootloader_dfu_keys.html \u003cbr /\u003e\r\n\r\n\r\n### scripts/package_testapp and package_testapp_debug  \u003cbr /\u003e\r\nThis is a bash script. Double click it or copy the nrfutil command out of it. \u003cbr /\u003e\r\nUses \"nrfutil pkg generate\" to make a zip package combining testapp.hex with the private key. \u003cbr /\u003e\r\nThe zip contains a bin file, which is simply the hex converted to bin and a dat file which provides the init packet and contains the signature. \u003cbr /\u003e\r\nhttps://infocenter.nordicsemi.com/topic/sdk_nrf5_v16.0.0/lib_bootloader_dfu_validation.html \u003cbr /\u003e\r\nLooking at the code in nrf_dfu_ver_validation_check makes the documentation clearer! \u003cbr /\u003e\r\n\r\n\r\n### scripts/merge_testapp and merge_testapp_debug \u003cbr /\u003e\r\nThis is a bash script. Double click it or copy the nrfutil command out of it. \u003cbr /\u003e\r\nUses \"nrfutil settings generate\" to make a bootloader settings page hex for the application.hex. \u003cbr /\u003e\r\nUses \"mergehex\" to merge the softdevice, application, bootloader and settings page. \u003cbr /\u003e\r\n\r\n## Enabling logging in the bootloader\r\n\r\ngit patch:\r\n\r\n```\r\ndiff --git bootloader/main.c bootloader/main.c\r\nindex fa4cf3b..71c6dd6 100755\r\n--- bootloader/main.c\r\n+++ bootloader/main.c\r\n@@ -46,6 +46,11 @@\r\n  *\r\n  */\r\n \r\n+#define NRF_LOG_ENABLED 1\r\n+#define NRF_LOG_STR_FORMATTER_TIMESTAMP_FORMAT_ENABLED 0 \r\n+#define NRF_LOG_DEFAULT_LEVEL 1\r\n+\r\n+\r\n #include \u003cstdint.h\u003e\r\n #include \"nrf_mbr.h\"\r\n #include \"nrf_bootloader.h\"\r\ndiff --git bootloader/microbit/armgcc/Makefile bootloader/microbit/armgcc/Makefile\r\nindex fae7c00..4a6e1d5 100755\r\n--- bootloader/microbit/armgcc/Makefile\r\n+++ bootloader/microbit/armgcc/Makefile\r\n@@ -87,6 +87,13 @@ SRC_FILES += \\\r\n   $(SDK_ROOT)/components/libraries/crypto/backend/oberon/oberon_backend_eddsa.c \\\r\n   $(SDK_ROOT)/components/libraries/crypto/backend/oberon/oberon_backend_hash.c \\\r\n   $(SDK_ROOT)/components/libraries/crypto/backend/oberon/oberon_backend_hmac.c \\\r\n+  $(SDK_ROOT)/components/libraries/log/src/nrf_log_backend_flash.c \\\r\n+  $(SDK_ROOT)/components/libraries/log/src/nrf_log_backend_rtt.c \\\r\n+  $(SDK_ROOT)/components/libraries/log/src/nrf_log_backend_serial.c \\\r\n+  $(SDK_ROOT)/components/libraries/log/src/nrf_log_backend_uart.c \\\r\n+  $(SDK_ROOT)/components/libraries/log/src/nrf_log_default_backends.c \\\r\n+  $(SDK_ROOT)/components/libraries/log/src/nrf_log_frontend.c \\\r\n+  $(SDK_ROOT)/components/libraries/memobj/nrf_memobj.c \\\r\n \r\n # Include folders common to all targets\r\n INC_FOLDERS += \\\r\n@@ -140,6 +147,7 @@ INC_FOLDERS += \\\r\n   $(SDK_ROOT)/external/nano-pb \\\r\n   $(SDK_ROOT)/components/libraries/queue \\\r\n   $(SDK_ROOT)/components/libraries/ringbuf \\\r\n+  $(SDK_ROOT)/external/fprintf/ \\\r\n \r\n # Libraries common to all targets\r\n LIB_FILES += \\\r\ndiff --git bootloader/microbit/config/sdk_config.h bootloader/microbit/config/sdk_config.h\r\nindex 3519dac..b119319 100755\r\n--- bootloader/microbit/config/sdk_config.h\r\n+++ bootloader/microbit/config/sdk_config.h\r\n@@ -1701,7 +1701,7 @@\r\n // \u003ce\u003e NRF_LOG_ENABLED - nrf_log - Logger\r\n //==========================================================\r\n #ifndef NRF_LOG_ENABLED\r\n-#define NRF_LOG_ENABLED 0\r\n+#define NRF_LOG_ENABLED 1\r\n #endif\r\n // \u003ch\u003e Log message pool - Configuration of log message pool\r\n \r\n```\r\n\r\n# License\r\n\r\nThis repository contains code with different licenses\r\n\r\nThe modified Nordic SDK and examples are licensed by Nordic Semiconductor ASA. Each of these files contains it's own license.\r\nAny other files were created by the Micro:bit Educational Foundation and are [MIT licensed](LICENSE).\r\n\r\n# Code of Conduct\r\n\r\nTrust, partnership, simplicity and passion are our core values we live and breathe in our daily work life and within our projects. Our open-source projects are no exception. We have an active community which spans the globe and we welcome and encourage participation and contributions to our projects by everyone. We work to foster a positive, open, inclusive and supportive environment and trust that our community respects the micro:bit code of conduct. Please see our [code of conduct](https://microbit.org/safeguarding/) which outlines our expectations for all those that participate in our community and details on how to report any concerns and what would happen should breaches occur.\r\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicrobit-foundation%2Fv2-bootloader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmicrobit-foundation%2Fv2-bootloader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicrobit-foundation%2Fv2-bootloader/lists"}