Gustavo Henrique Nihei | 7bcf986 | 2021-07-26 14:39:16 -0300 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * boot/nuttx/main.c |
| 3 | * |
| 4 | * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | ****************************************************************************/ |
| 19 | |
| 20 | /**************************************************************************** |
| 21 | * Included Files |
| 22 | ****************************************************************************/ |
| 23 | |
| 24 | #include <nuttx/config.h> |
| 25 | |
Gerson Fernando Budke | d930731 | 2021-12-20 19:51:38 -0300 | [diff] [blame] | 26 | #include <syslog.h> |
Gustavo Henrique Nihei | 7bcf986 | 2021-07-26 14:39:16 -0300 | [diff] [blame] | 27 | |
| 28 | #include <sys/boardctl.h> |
| 29 | |
| 30 | #include <bootutil/bootutil.h> |
| 31 | #include <bootutil/image.h> |
| 32 | |
| 33 | #include "flash_map_backend/flash_map_backend.h" |
| 34 | |
| 35 | /**************************************************************************** |
Andrés Sánchez Pascual | 414ac87 | 2022-08-28 18:43:02 +0200 | [diff] [blame] | 36 | * Pre-processor Definitions |
| 37 | ****************************************************************************/ |
| 38 | |
| 39 | /* Should we perform board-specific driver initialization? There are two |
| 40 | * ways that board initialization can occur: 1) automatically via |
| 41 | * board_late_initialize() during bootupif CONFIG_BOARD_LATE_INITIALIZE |
| 42 | * or 2). |
| 43 | * via a call to boardctl() if the interface is enabled |
| 44 | * (CONFIG_BOARDCTL=y). |
| 45 | * If this task is running as an NSH built-in application, then that |
| 46 | * initialization has probably already been performed otherwise we do it |
| 47 | * here. |
| 48 | */ |
| 49 | |
| 50 | #undef NEED_BOARDINIT |
| 51 | |
| 52 | #if defined(CONFIG_BOARDCTL) && !defined(CONFIG_NSH_ARCHINIT) |
| 53 | # define NEED_BOARDINIT 1 |
| 54 | #endif |
| 55 | |
| 56 | /**************************************************************************** |
Gustavo Henrique Nihei | 7bcf986 | 2021-07-26 14:39:16 -0300 | [diff] [blame] | 57 | * Private Functions |
| 58 | ****************************************************************************/ |
| 59 | |
| 60 | /**************************************************************************** |
| 61 | * do_boot |
| 62 | ****************************************************************************/ |
| 63 | |
| 64 | static void do_boot(struct boot_rsp *rsp) |
| 65 | { |
Gustavo Henrique Nihei | fca1aa4 | 2021-08-20 10:30:44 -0300 | [diff] [blame] | 66 | const struct flash_area *flash_area; |
Gustavo Henrique Nihei | 7bcf986 | 2021-07-26 14:39:16 -0300 | [diff] [blame] | 67 | struct boardioc_boot_info_s info; |
| 68 | int area_id; |
| 69 | int ret; |
| 70 | |
| 71 | area_id = flash_area_id_from_image_offset(rsp->br_image_off); |
| 72 | |
| 73 | ret = flash_area_open(area_id, &flash_area); |
| 74 | assert(ret == OK); |
| 75 | |
Gerson Fernando Budke | d930731 | 2021-12-20 19:51:38 -0300 | [diff] [blame] | 76 | syslog(LOG_INFO, "Booting from %s...\n", flash_area->fa_mtd_path); |
Gustavo Henrique Nihei | 7bcf986 | 2021-07-26 14:39:16 -0300 | [diff] [blame] | 77 | |
| 78 | info.path = flash_area->fa_mtd_path; |
| 79 | info.header_size = rsp->br_hdr->ih_hdr_size; |
| 80 | |
| 81 | flash_area_close(flash_area); |
| 82 | |
Gustavo Henrique Nihei | fca1aa4 | 2021-08-20 10:30:44 -0300 | [diff] [blame] | 83 | if (boardctl(BOARDIOC_BOOT_IMAGE, (uintptr_t)&info) != OK) |
Gustavo Henrique Nihei | 7bcf986 | 2021-07-26 14:39:16 -0300 | [diff] [blame] | 84 | { |
Gerson Fernando Budke | d930731 | 2021-12-20 19:51:38 -0300 | [diff] [blame] | 85 | syslog(LOG_ERR, "Failed to load application image!\n"); |
Gustavo Henrique Nihei | 7bcf986 | 2021-07-26 14:39:16 -0300 | [diff] [blame] | 86 | FIH_PANIC; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /**************************************************************************** |
| 91 | * Public Functions |
| 92 | ****************************************************************************/ |
| 93 | |
| 94 | /**************************************************************************** |
| 95 | * main |
| 96 | ****************************************************************************/ |
| 97 | |
| 98 | int main(int argc, FAR char *argv[]) |
| 99 | { |
| 100 | struct boot_rsp rsp; |
| 101 | fih_int fih_rc = FIH_FAILURE; |
| 102 | |
Andrés Sánchez Pascual | 414ac87 | 2022-08-28 18:43:02 +0200 | [diff] [blame] | 103 | #ifdef NEED_BOARDINIT |
| 104 | /* Perform architecture-specific initialization (if configured) */ |
| 105 | |
| 106 | boardctl(BOARDIOC_INIT, 0); |
| 107 | |
| 108 | #ifdef CONFIG_BOARDCTL_FINALINIT |
| 109 | /* Perform architecture-specific final-initialization (if configured) */ |
| 110 | |
| 111 | boardctl(BOARDIOC_FINALINIT, 0); |
| 112 | #endif |
| 113 | #endif |
| 114 | |
Gerson Fernando Budke | d930731 | 2021-12-20 19:51:38 -0300 | [diff] [blame] | 115 | syslog(LOG_INFO, "*** Booting MCUboot build %s ***\n", CONFIG_MCUBOOT_VERSION); |
Gustavo Henrique Nihei | 7bcf986 | 2021-07-26 14:39:16 -0300 | [diff] [blame] | 116 | |
Andrés Sánchez Pascual | 6ea3e9b | 2022-11-07 18:09:25 +0100 | [diff] [blame^] | 117 | #ifdef CONFIG_MCUBOOT_WATCHDOG |
| 118 | int ret = mcuboot_watchdog_init(); |
| 119 | if (ret < 0) |
| 120 | { |
| 121 | syslog(LOG_ERR, "Unable to initialize the watchdog timer\n"); |
| 122 | FIH_PANIC; |
| 123 | } |
| 124 | #endif |
| 125 | |
Gustavo Henrique Nihei | 7bcf986 | 2021-07-26 14:39:16 -0300 | [diff] [blame] | 126 | FIH_CALL(boot_go, fih_rc, &rsp); |
| 127 | |
| 128 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) |
| 129 | { |
Gerson Fernando Budke | d930731 | 2021-12-20 19:51:38 -0300 | [diff] [blame] | 130 | syslog(LOG_ERR, "Unable to find bootable image\n"); |
Gustavo Henrique Nihei | 7bcf986 | 2021-07-26 14:39:16 -0300 | [diff] [blame] | 131 | FIH_PANIC; |
| 132 | } |
| 133 | |
| 134 | do_boot(&rsp); |
| 135 | |
| 136 | while (1); |
| 137 | } |