blob: 083d03e3f8fdbb8262a1eb94faf5d821222d9474 [file] [log] [blame]
Christopher Collinscf18a032017-02-06 15:10:45 -08001/*
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 Utzig1a2e41a2017-11-17 12:13:09 -020020#include "mcuboot_config/mcuboot_config.h"
Fabio Utzig1a2e41a2017-11-17 12:13:09 -020021
Christopher Collinscf18a032017-02-06 15:10:45 -080022#include <assert.h>
23#include <stddef.h>
24#include <inttypes.h>
Fabio Utzig1d46c942018-02-26 10:38:00 -030025#include <stdio.h>
Christopher Collinscf18a032017-02-06 15:10:45 -080026#include "syscfg/syscfg.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020027#include <flash_map_backend/flash_map_backend.h>
Christopher Collinscf18a032017-02-06 15:10:45 -080028#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 Utzig1d46c942018-02-26 10:38:00 -030033#include <sysinit/sysinit.h>
Fabio Utzig19356bf2017-05-11 16:19:36 -030034#ifdef MCUBOOT_SERIAL
Christopher Collinscf18a032017-02-06 15:10:45 -080035#include <hal/hal_gpio.h>
Marko Kiiskila316d3612018-06-05 12:03:27 +030036#include <hal/hal_nvreg.h>
Christopher Collinscf18a032017-02-06 15:10:45 -080037#include <boot_serial/boot_serial.h>
Christopher Collinscf18a032017-02-06 15:10:45 -080038#endif
39#include <console/console.h>
40#include "bootutil/image.h"
41#include "bootutil/bootutil.h"
Fabio Utzig94912c52018-05-07 08:38:23 -030042#include "bootutil/bootutil_log.h"
Christopher Collinscf18a032017-02-06 15:10:45 -080043
Fabio Utzig12f819f2018-03-29 16:20:53 -030044#define BOOT_AREA_DESC_MAX (256)
45#define AREA_DESC_MAX (BOOT_AREA_DESC_MAX)
Christopher Collinscf18a032017-02-06 15:10:45 -080046
Fabio Utzig19356bf2017-05-11 16:19:36 -030047#ifdef MCUBOOT_SERIAL
Marko Kiiskila316d3612018-06-05 12:03:27 +030048#define BOOT_SERIAL_INPUT_MAX (512)
Christopher Collinscf18a032017-02-06 15:10:45 -080049#endif
50
Marti Bolivareb940802017-05-01 23:15:29 -040051/*
52 * Temporary flash_device_base() implementation.
53 *
54 * TODO: remove this when mynewt needs to support flash_device_base()
55 * for devices with nonzero base addresses.
56 */
57int flash_device_base(uint8_t fd_id, uintptr_t *ret)
58{
59 *ret = 0;
60 return 0;
61}
62
Marko Kiiskila316d3612018-06-05 12:03:27 +030063#ifdef MCUBOOT_SERIAL
64static void
65serial_boot_detect(void)
66{
67 /*
68 * Read retained register and compare with expected magic value.
69 * If it matches, await for download commands from serial.
70 */
71#if MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX) != -1
72 if (hal_nvreg_read(MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX)) ==
73 MYNEWT_VAL(BOOT_SERIAL_NVREG_MAGIC)) {
74
75 hal_nvreg_write(MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX), 0);
76
77 boot_serial_start(BOOT_SERIAL_INPUT_MAX);
78 assert(0);
79 }
80
81#endif
82
83 /*
84 * Configure a GPIO as input, and compare it against expected value.
85 * If it matches, await for download commands from serial.
86 */
87#if MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN) != -1
88 hal_gpio_init_in(MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN),
89 MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN_CFG));
90 if (hal_gpio_read(MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN)) ==
91 MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN_VAL)) {
92 boot_serial_start(BOOT_SERIAL_INPUT_MAX);
93 assert(0);
94 }
95#endif
96
97 /*
98 * Listen for management pattern in UART input. If detected, await for
99 * download commands from serial.
100 */
101#if MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) != 0
102 if (boot_serial_detect_uart_string()) {
103 boot_serial_start(BOOT_SERIAL_INPUT_MAX);
104 assert(0);
105 }
106#endif
107}
108#endif
109
Christopher Collinscf18a032017-02-06 15:10:45 -0800110int
111main(void)
112{
113 struct boot_rsp rsp;
Marti Bolivareb940802017-05-01 23:15:29 -0400114 uintptr_t flash_base;
Christopher Collinscf18a032017-02-06 15:10:45 -0800115 int rc;
116
Andrzej Kaczmarekc49099c2018-02-16 17:10:51 +0100117 hal_bsp_init();
Fabio Utzig1d46c942018-02-26 10:38:00 -0300118
Fabio Utzig94912c52018-05-07 08:38:23 -0300119#if defined(MCUBOOT_SERIAL) || defined(MCUBOOT_HAVE_LOGGING)
Fabio Utzig9f7c3d22018-02-16 14:55:28 -0200120 /* initialize uart without os */
121 os_dev_initialize_all(OS_DEV_INIT_PRIMARY);
Marko Kiiskila316d3612018-06-05 12:03:27 +0300122 os_dev_initialize_all(OS_DEV_INIT_SECONDARY);
Christopher Collinscf18a032017-02-06 15:10:45 -0800123 sysinit();
Fabio Utzig1d46c942018-02-26 10:38:00 -0300124 console_blocking_mode();
Marko Kiiskila316d3612018-06-05 12:03:27 +0300125#if defined(MCUBOOT_SERIAL)
126 serial_boot_detect();
127#endif
Fabio Utzig94912c52018-05-07 08:38:23 -0300128#else
129 flash_map_init();
130#endif
Christopher Collinscf18a032017-02-06 15:10:45 -0800131
Christopher Collinscf18a032017-02-06 15:10:45 -0800132 rc = boot_go(&rsp);
133 assert(rc == 0);
134
Fabio Utzigb00d6482017-06-20 19:28:22 -0300135 rc = flash_device_base(rsp.br_flash_dev_id, &flash_base);
Marti Bolivareb940802017-05-01 23:15:29 -0400136 assert(rc == 0);
137
138 hal_system_start((void *)(flash_base + rsp.br_image_off +
139 rsp.br_hdr->ih_hdr_size));
Christopher Collinscf18a032017-02-06 15:10:45 -0800140
141 return 0;
142}