Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
Fabio Utzig | 1a2e41a | 2017-11-17 12:13:09 -0200 | [diff] [blame] | 20 | #include "mcuboot_config/mcuboot_config.h" |
Fabio Utzig | 1a2e41a | 2017-11-17 12:13:09 -0200 | [diff] [blame] | 21 | |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 22 | #include <assert.h> |
| 23 | #include <stddef.h> |
| 24 | #include <inttypes.h> |
Fabio Utzig | 1d46c94 | 2018-02-26 10:38:00 -0300 | [diff] [blame] | 25 | #include <stdio.h> |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 26 | #include "syscfg/syscfg.h" |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame^] | 27 | #include <flash_map_backend/flash_map_backend.h> |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 28 | #include <os/os.h> |
| 29 | #include <bsp/bsp.h> |
| 30 | #include <hal/hal_bsp.h> |
| 31 | #include <hal/hal_system.h> |
| 32 | #include <hal/hal_flash.h> |
Fabio Utzig | 1d46c94 | 2018-02-26 10:38:00 -0300 | [diff] [blame] | 33 | #include <sysinit/sysinit.h> |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 34 | #ifdef MCUBOOT_SERIAL |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 35 | #include <hal/hal_gpio.h> |
| 36 | #include <boot_serial/boot_serial.h> |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 37 | #endif |
| 38 | #include <console/console.h> |
| 39 | #include "bootutil/image.h" |
| 40 | #include "bootutil/bootutil.h" |
| 41 | |
Fabio Utzig | 12f819f | 2018-03-29 16:20:53 -0300 | [diff] [blame] | 42 | #define BOOT_AREA_DESC_MAX (256) |
| 43 | #define AREA_DESC_MAX (BOOT_AREA_DESC_MAX) |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 44 | |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 45 | #ifdef MCUBOOT_SERIAL |
Fabio Utzig | 12f819f | 2018-03-29 16:20:53 -0300 | [diff] [blame] | 46 | #define BOOT_SER_CONS_INPUT 256 |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 47 | #endif |
| 48 | |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 49 | /* |
| 50 | * Temporary flash_device_base() implementation. |
| 51 | * |
| 52 | * TODO: remove this when mynewt needs to support flash_device_base() |
| 53 | * for devices with nonzero base addresses. |
| 54 | */ |
| 55 | int flash_device_base(uint8_t fd_id, uintptr_t *ret) |
| 56 | { |
| 57 | *ret = 0; |
| 58 | return 0; |
| 59 | } |
| 60 | |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 61 | int |
| 62 | main(void) |
| 63 | { |
| 64 | struct boot_rsp rsp; |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 65 | uintptr_t flash_base; |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 66 | int rc; |
| 67 | |
Andrzej Kaczmarek | c49099c | 2018-02-16 17:10:51 +0100 | [diff] [blame] | 68 | hal_bsp_init(); |
Fabio Utzig | 1d46c94 | 2018-02-26 10:38:00 -0300 | [diff] [blame] | 69 | |
Fabio Utzig | 9f7c3d2 | 2018-02-16 14:55:28 -0200 | [diff] [blame] | 70 | /* initialize uart without os */ |
| 71 | os_dev_initialize_all(OS_DEV_INIT_PRIMARY); |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 72 | sysinit(); |
Fabio Utzig | 1d46c94 | 2018-02-26 10:38:00 -0300 | [diff] [blame] | 73 | console_blocking_mode(); |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 74 | |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 75 | #ifdef MCUBOOT_SERIAL |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 76 | /* |
| 77 | * Configure a GPIO as input, and compare it against expected value. |
| 78 | * If it matches, await for download commands from serial. |
| 79 | */ |
Fabio Utzig | 1a2e41a | 2017-11-17 12:13:09 -0200 | [diff] [blame] | 80 | hal_gpio_init_in(MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN), |
| 81 | MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN_CFG)); |
| 82 | if (hal_gpio_read(MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN)) == |
| 83 | MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN_VAL)) { |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 84 | boot_serial_start(BOOT_SER_CONS_INPUT); |
| 85 | assert(0); |
| 86 | } |
| 87 | #endif |
Fabio Utzig | 1d46c94 | 2018-02-26 10:38:00 -0300 | [diff] [blame] | 88 | |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 89 | rc = boot_go(&rsp); |
| 90 | assert(rc == 0); |
| 91 | |
Fabio Utzig | b00d648 | 2017-06-20 19:28:22 -0300 | [diff] [blame] | 92 | rc = flash_device_base(rsp.br_flash_dev_id, &flash_base); |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 93 | assert(rc == 0); |
| 94 | |
| 95 | hal_system_start((void *)(flash_base + rsp.br_image_off + |
| 96 | rsp.br_hdr->ih_hdr_size)); |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 97 | |
| 98 | return 0; |
| 99 | } |