blob: b9f4e83110ae7b2686205d173d673777d48a0f53 [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#ifdef MCUBOOT_MYNEWT
21#include "mcuboot_config/mcuboot_config.h"
22#endif
23
Christopher Collinscf18a032017-02-06 15:10:45 -080024#include <assert.h>
25#include <stddef.h>
26#include <inttypes.h>
Fabio Utzig1d46c942018-02-26 10:38:00 -030027#include <stdio.h>
Christopher Collinscf18a032017-02-06 15:10:45 -080028#include "syscfg/syscfg.h"
29#include <flash_map/flash_map.h>
30#include <os/os.h>
31#include <bsp/bsp.h>
32#include <hal/hal_bsp.h>
33#include <hal/hal_system.h>
34#include <hal/hal_flash.h>
Fabio Utzig1d46c942018-02-26 10:38:00 -030035#include <sysinit/sysinit.h>
Fabio Utzig19356bf2017-05-11 16:19:36 -030036#ifdef MCUBOOT_SERIAL
Christopher Collinscf18a032017-02-06 15:10:45 -080037#include <hal/hal_gpio.h>
38#include <boot_serial/boot_serial.h>
Christopher Collinscf18a032017-02-06 15:10:45 -080039#endif
40#include <console/console.h>
41#include "bootutil/image.h"
42#include "bootutil/bootutil.h"
43
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
Fabio Utzig12f819f2018-03-29 16:20:53 -030048#define BOOT_SER_CONS_INPUT 256
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
Christopher Collinscf18a032017-02-06 15:10:45 -080063int
64main(void)
65{
66 struct boot_rsp rsp;
Marti Bolivareb940802017-05-01 23:15:29 -040067 uintptr_t flash_base;
Christopher Collinscf18a032017-02-06 15:10:45 -080068 int rc;
69
Andrzej Kaczmarekc49099c2018-02-16 17:10:51 +010070 hal_bsp_init();
Fabio Utzig1d46c942018-02-26 10:38:00 -030071
Fabio Utzig9f7c3d22018-02-16 14:55:28 -020072 /* initialize uart without os */
73 os_dev_initialize_all(OS_DEV_INIT_PRIMARY);
Christopher Collinscf18a032017-02-06 15:10:45 -080074 sysinit();
Fabio Utzig1d46c942018-02-26 10:38:00 -030075 console_blocking_mode();
Christopher Collinscf18a032017-02-06 15:10:45 -080076
Fabio Utzig19356bf2017-05-11 16:19:36 -030077#ifdef MCUBOOT_SERIAL
Christopher Collinscf18a032017-02-06 15:10:45 -080078 /*
79 * Configure a GPIO as input, and compare it against expected value.
80 * If it matches, await for download commands from serial.
81 */
Fabio Utzig1a2e41a2017-11-17 12:13:09 -020082 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)) {
Christopher Collinscf18a032017-02-06 15:10:45 -080086 boot_serial_start(BOOT_SER_CONS_INPUT);
87 assert(0);
88 }
89#endif
Fabio Utzig1d46c942018-02-26 10:38:00 -030090
Christopher Collinscf18a032017-02-06 15:10:45 -080091 rc = boot_go(&rsp);
92 assert(rc == 0);
93
Fabio Utzigb00d6482017-06-20 19:28:22 -030094 rc = flash_device_base(rsp.br_flash_dev_id, &flash_base);
Marti Bolivareb940802017-05-01 23:15:29 -040095 assert(rc == 0);
96
97 hal_system_start((void *)(flash_base + rsp.br_image_off +
98 rsp.br_hdr->ih_hdr_size));
Christopher Collinscf18a032017-02-06 15:10:45 -080099
100 return 0;
101}