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 | |
| 26 | #include <stdio.h> |
| 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 | /**************************************************************************** |
| 36 | * Private Functions |
| 37 | ****************************************************************************/ |
| 38 | |
| 39 | /**************************************************************************** |
| 40 | * do_boot |
| 41 | ****************************************************************************/ |
| 42 | |
| 43 | static void do_boot(struct boot_rsp *rsp) |
| 44 | { |
| 45 | struct flash_area *flash_area; |
| 46 | struct boardioc_boot_info_s info; |
| 47 | int area_id; |
| 48 | int ret; |
| 49 | |
| 50 | area_id = flash_area_id_from_image_offset(rsp->br_image_off); |
| 51 | |
| 52 | ret = flash_area_open(area_id, &flash_area); |
| 53 | assert(ret == OK); |
| 54 | |
| 55 | printf("Booting from %s...\n", flash_area->fa_mtd_path); |
| 56 | |
| 57 | info.path = flash_area->fa_mtd_path; |
| 58 | info.header_size = rsp->br_hdr->ih_hdr_size; |
| 59 | |
| 60 | flash_area_close(flash_area); |
| 61 | |
| 62 | if (boardctl(BOARDIOC_BOOT_IMAGE, &info) != OK) |
| 63 | { |
| 64 | fprintf(stderr, "Failed to load application image!\n"); |
| 65 | FIH_PANIC; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /**************************************************************************** |
| 70 | * Public Functions |
| 71 | ****************************************************************************/ |
| 72 | |
| 73 | /**************************************************************************** |
| 74 | * main |
| 75 | ****************************************************************************/ |
| 76 | |
| 77 | int main(int argc, FAR char *argv[]) |
| 78 | { |
| 79 | struct boot_rsp rsp; |
| 80 | fih_int fih_rc = FIH_FAILURE; |
| 81 | |
| 82 | printf("*** Booting MCUboot build %s ***\n", CONFIG_MCUBOOT_VERSION); |
| 83 | |
| 84 | FIH_CALL(boot_go, fih_rc, &rsp); |
| 85 | |
| 86 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) |
| 87 | { |
| 88 | fprintf(stderr, "Unable to find bootable image\n"); |
| 89 | FIH_PANIC; |
| 90 | } |
| 91 | |
| 92 | do_boot(&rsp); |
| 93 | |
| 94 | while (1); |
| 95 | } |