Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2020, STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
| 8 | #include <dfu.h> |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame^] | 9 | #include <asm/arch/stm32prog.h> |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 10 | #include "stm32prog.h" |
| 11 | |
| 12 | struct stm32prog_data *stm32prog_data; |
| 13 | |
| 14 | static int do_stm32prog(cmd_tbl_t *cmdtp, int flag, int argc, |
| 15 | char * const argv[]) |
| 16 | { |
| 17 | ulong addr, size; |
| 18 | int dev, ret; |
| 19 | enum stm32prog_link_t link = LINK_UNDEFINED; |
| 20 | bool reset = false; |
| 21 | struct stm32prog_data *data; |
| 22 | |
| 23 | if (argc < 3 || argc > 5) |
| 24 | return CMD_RET_USAGE; |
| 25 | |
| 26 | if (!strcmp(argv[1], "usb")) |
| 27 | link = LINK_USB; |
| 28 | |
| 29 | if (link == LINK_UNDEFINED) { |
| 30 | pr_err("not supported link=%s\n", argv[1]); |
| 31 | return CMD_RET_USAGE; |
| 32 | } |
| 33 | dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 34 | |
| 35 | addr = STM32_DDR_BASE; |
| 36 | size = 0; |
| 37 | if (argc > 3) { |
| 38 | addr = simple_strtoul(argv[3], NULL, 16); |
| 39 | if (!addr) |
| 40 | return CMD_RET_FAILURE; |
| 41 | } |
| 42 | if (argc > 4) |
| 43 | size = simple_strtoul(argv[4], NULL, 16); |
| 44 | |
| 45 | data = (struct stm32prog_data *)malloc(sizeof(*data)); |
| 46 | |
| 47 | if (!data) { |
| 48 | pr_err("Alloc failed."); |
| 49 | return CMD_RET_FAILURE; |
| 50 | } |
| 51 | stm32prog_data = data; |
| 52 | |
| 53 | ret = stm32prog_init(data, addr, size); |
| 54 | if (ret) |
| 55 | printf("Invalid or missing layout file."); |
| 56 | |
| 57 | /* prepare DFU for device read/write */ |
| 58 | ret = stm32prog_dfu_init(data); |
| 59 | if (ret) |
| 60 | goto cleanup; |
| 61 | |
| 62 | switch (link) { |
| 63 | case LINK_USB: |
| 64 | reset = stm32prog_usb_loop(data, dev); |
| 65 | break; |
| 66 | default: |
| 67 | goto cleanup; |
| 68 | } |
| 69 | |
| 70 | stm32prog_clean(data); |
| 71 | free(stm32prog_data); |
| 72 | stm32prog_data = NULL; |
| 73 | |
| 74 | puts("Download done\n"); |
| 75 | if (reset) { |
| 76 | puts("Reset...\n"); |
| 77 | run_command("reset", 0); |
| 78 | } |
| 79 | |
| 80 | return CMD_RET_SUCCESS; |
| 81 | |
| 82 | cleanup: |
| 83 | stm32prog_clean(data); |
| 84 | free(stm32prog_data); |
| 85 | stm32prog_data = NULL; |
| 86 | |
| 87 | return CMD_RET_FAILURE; |
| 88 | } |
| 89 | |
| 90 | U_BOOT_CMD(stm32prog, 5, 0, do_stm32prog, |
| 91 | "<link> <dev> [<addr>] [<size>]\n" |
| 92 | "start communication with tools STM32Cubeprogrammer on <link> with Flashlayout at <addr>", |
| 93 | "<link> = usb\n" |
| 94 | "<dev> = device instance\n" |
| 95 | "<addr> = address of flashlayout\n" |
| 96 | "<size> = size of flashlayout\n" |
| 97 | ); |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame^] | 98 | |
| 99 | bool stm32prog_get_tee_partitions(void) |
| 100 | { |
| 101 | if (stm32prog_data) |
| 102 | return stm32prog_data->tee_detected; |
| 103 | |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | bool stm32prog_get_fsbl_nor(void) |
| 108 | { |
| 109 | if (stm32prog_data) |
| 110 | return stm32prog_data->fsbl_nor_detected; |
| 111 | |
| 112 | return false; |
| 113 | } |