blob: 8973053bf51bc8438512ad040486a03c51a788e9 [file] [log] [blame]
Gustavo Henrique Nihei7bcf9862021-07-26 14:39:16 -03001/****************************************************************************
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 Budked9307312021-12-20 19:51:38 -030026#include <syslog.h>
Gustavo Henrique Nihei7bcf9862021-07-26 14:39:16 -030027
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 Pascual414ac872022-08-28 18:43:02 +020036 * 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 Nihei7bcf9862021-07-26 14:39:16 -030057 * Private Functions
58 ****************************************************************************/
59
60/****************************************************************************
61 * do_boot
62 ****************************************************************************/
63
64static void do_boot(struct boot_rsp *rsp)
65{
Gustavo Henrique Niheifca1aa42021-08-20 10:30:44 -030066 const struct flash_area *flash_area;
Gustavo Henrique Nihei7bcf9862021-07-26 14:39:16 -030067 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 Budked9307312021-12-20 19:51:38 -030076 syslog(LOG_INFO, "Booting from %s...\n", flash_area->fa_mtd_path);
Gustavo Henrique Nihei7bcf9862021-07-26 14:39:16 -030077
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 Niheifca1aa42021-08-20 10:30:44 -030083 if (boardctl(BOARDIOC_BOOT_IMAGE, (uintptr_t)&info) != OK)
Gustavo Henrique Nihei7bcf9862021-07-26 14:39:16 -030084 {
Gerson Fernando Budked9307312021-12-20 19:51:38 -030085 syslog(LOG_ERR, "Failed to load application image!\n");
Gustavo Henrique Nihei7bcf9862021-07-26 14:39:16 -030086 FIH_PANIC;
87 }
88}
89
90/****************************************************************************
91 * Public Functions
92 ****************************************************************************/
93
94/****************************************************************************
95 * main
96 ****************************************************************************/
97
98int main(int argc, FAR char *argv[])
99{
100 struct boot_rsp rsp;
101 fih_int fih_rc = FIH_FAILURE;
102
Andrés Sánchez Pascual414ac872022-08-28 18:43:02 +0200103#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 Budked9307312021-12-20 19:51:38 -0300115 syslog(LOG_INFO, "*** Booting MCUboot build %s ***\n", CONFIG_MCUBOOT_VERSION);
Gustavo Henrique Nihei7bcf9862021-07-26 14:39:16 -0300116
117 FIH_CALL(boot_go, fih_rc, &rsp);
118
119 if (fih_not_eq(fih_rc, FIH_SUCCESS))
120 {
Gerson Fernando Budked9307312021-12-20 19:51:38 -0300121 syslog(LOG_ERR, "Unable to find bootable image\n");
Gustavo Henrique Nihei7bcf9862021-07-26 14:39:16 -0300122 FIH_PANIC;
123 }
124
125 do_boot(&rsp);
126
127 while (1);
128}