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 | #ifdef MCUBOOT_MYNEWT |
| 21 | #include "mcuboot_config/mcuboot_config.h" |
| 22 | #endif |
| 23 | |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <stddef.h> |
| 26 | #include <inttypes.h> |
| 27 | #include "syscfg/syscfg.h" |
| 28 | #include <flash_map/flash_map.h> |
| 29 | #include <os/os.h> |
| 30 | #include <bsp/bsp.h> |
| 31 | #include <hal/hal_bsp.h> |
| 32 | #include <hal/hal_system.h> |
| 33 | #include <hal/hal_flash.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> |
| 37 | #include <sysinit/sysinit.h> |
| 38 | #endif |
| 39 | #include <console/console.h> |
| 40 | #include "bootutil/image.h" |
| 41 | #include "bootutil/bootutil.h" |
| 42 | |
| 43 | #define BOOT_AREA_DESC_MAX (256) |
| 44 | #define AREA_DESC_MAX (BOOT_AREA_DESC_MAX) |
| 45 | |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 46 | #ifdef MCUBOOT_SERIAL |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 47 | #define BOOT_SER_CONS_INPUT 128 |
| 48 | #endif |
| 49 | |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 50 | /* |
| 51 | * Temporary flash_device_base() implementation. |
| 52 | * |
| 53 | * TODO: remove this when mynewt needs to support flash_device_base() |
| 54 | * for devices with nonzero base addresses. |
| 55 | */ |
| 56 | int flash_device_base(uint8_t fd_id, uintptr_t *ret) |
| 57 | { |
| 58 | *ret = 0; |
| 59 | return 0; |
| 60 | } |
| 61 | |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 62 | int |
| 63 | main(void) |
| 64 | { |
| 65 | struct boot_rsp rsp; |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 66 | uintptr_t flash_base; |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 67 | int rc; |
| 68 | |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 69 | #ifdef MCUBOOT_SERIAL |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 70 | sysinit(); |
| 71 | #else |
| 72 | flash_map_init(); |
| 73 | hal_bsp_init(); |
| 74 | #endif |
| 75 | |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 76 | #ifdef MCUBOOT_SERIAL |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 77 | /* |
| 78 | * Configure a GPIO as input, and compare it against expected value. |
| 79 | * If it matches, await for download commands from serial. |
| 80 | */ |
Fabio Utzig | 1a2e41a | 2017-11-17 12:13:09 -0200 | [diff] [blame^] | 81 | #ifdef MCUBOOT_MYNEWT |
| 82 | hal_gpio_init_in(MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN), |
| 83 | MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN_CFG)); |
| 84 | if (hal_gpio_read(MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN)) == |
| 85 | MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN_VAL)) { |
| 86 | #else |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 87 | hal_gpio_init_in(BOOT_SERIAL_DETECT_PIN, BOOT_SERIAL_DETECT_PIN_CFG); |
| 88 | if (hal_gpio_read(BOOT_SERIAL_DETECT_PIN) == BOOT_SERIAL_DETECT_PIN_VAL) { |
Fabio Utzig | 1a2e41a | 2017-11-17 12:13:09 -0200 | [diff] [blame^] | 89 | #endif |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 90 | boot_serial_start(BOOT_SER_CONS_INPUT); |
| 91 | assert(0); |
| 92 | } |
| 93 | #endif |
| 94 | rc = boot_go(&rsp); |
| 95 | assert(rc == 0); |
| 96 | |
Fabio Utzig | b00d648 | 2017-06-20 19:28:22 -0300 | [diff] [blame] | 97 | rc = flash_device_base(rsp.br_flash_dev_id, &flash_base); |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 98 | assert(rc == 0); |
| 99 | |
| 100 | hal_system_start((void *)(flash_base + rsp.br_image_off + |
| 101 | rsp.br_hdr->ih_hdr_size)); |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 102 | |
| 103 | return 0; |
| 104 | } |