uboot/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
   2/*
   3 * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
   4 */
   5
   6#ifndef _STM32PROG_H_
   7#define _STM32PROG_H_
   8
   9/* - phase defines ------------------------------------------------*/
  10#define PHASE_FLASHLAYOUT       0x00
  11#define PHASE_FIRST_USER        0x10
  12#define PHASE_LAST_USER         0xF0
  13#define PHASE_CMD               0xF1
  14#define PHASE_OTP               0xF2
  15#define PHASE_PMIC              0xF4
  16#define PHASE_END               0xFE
  17#define PHASE_RESET             0xFF
  18#define PHASE_DO_RESET          0x1FF
  19
  20#define DEFAULT_ADDRESS         0xFFFFFFFF
  21
  22#define CMD_SIZE                512
  23/* SMC is only supported in SPMIN for STM32MP15x */
  24#ifdef CONFIG_STM32MP15x
  25#define OTP_SIZE_SMC            1024
  26#else
  27#define OTP_SIZE_SMC            0
  28#endif
  29#define OTP_SIZE_TA             776
  30#define PMIC_SIZE               8
  31
  32enum stm32prog_target {
  33        STM32PROG_NONE,
  34        STM32PROG_MMC,
  35        STM32PROG_NAND,
  36        STM32PROG_NOR,
  37        STM32PROG_SPI_NAND,
  38        STM32PROG_RAM
  39};
  40
  41enum stm32prog_link_t {
  42        LINK_SERIAL,
  43        LINK_USB,
  44        LINK_UNDEFINED,
  45};
  46
  47enum stm32prog_header_t {
  48        HEADER_NONE,
  49        HEADER_STM32IMAGE,
  50        HEADER_STM32IMAGE_V2,
  51        HEADER_FIP,
  52};
  53
  54struct image_header_s {
  55        enum stm32prog_header_t type;
  56        u32     image_checksum;
  57        u32     image_length;
  58        u32     length;
  59};
  60
  61struct stm32_header_v1 {
  62        u32 magic_number;
  63        u8 image_signature[64];
  64        u32 image_checksum;
  65        u32 header_version;
  66        u32 image_length;
  67        u32 image_entry_point;
  68        u32 reserved1;
  69        u32 load_address;
  70        u32 reserved2;
  71        u32 version_number;
  72        u32 option_flags;
  73        u32 ecdsa_algorithm;
  74        u8 ecdsa_public_key[64];
  75        u8 padding[83];
  76        u8 binary_type;
  77};
  78
  79struct stm32_header_v2 {
  80        u32 magic_number;
  81        u8 image_signature[64];
  82        u32 image_checksum;
  83        u32 header_version;
  84        u32 image_length;
  85        u32 image_entry_point;
  86        u32 reserved1;
  87        u32 load_address;
  88        u32 reserved2;
  89        u32 version_number;
  90        u32 extension_flags;
  91        u32 extension_headers_length;
  92        u32 binary_type;
  93        u8 padding[16];
  94        u32 extension_header_type;
  95        u32 extension_header_length;
  96        u8 extension_padding[376];
  97};
  98
  99/* partition type in flashlayout file */
 100enum stm32prog_part_type {
 101        PART_BINARY,
 102        PART_FIP,
 103        PART_SYSTEM,
 104        PART_FILESYSTEM,
 105        RAW_IMAGE,
 106};
 107
 108/* device information */
 109struct stm32prog_dev_t {
 110        enum stm32prog_target   target;
 111        char                    dev_id;
 112        u32                     erase_size;
 113        struct mmc              *mmc;
 114        struct mtd_info         *mtd;
 115        /* list of partition for this device / ordered in offset */
 116        struct list_head        part_list;
 117        bool                    full_update;
 118};
 119
 120/* partition information build from FlashLayout and device */
 121struct stm32prog_part_t {
 122        /* FlashLayout information */
 123        int                     option;
 124        int                     id;
 125        enum stm32prog_part_type part_type;
 126        enum stm32prog_target   target;
 127        char                    dev_id;
 128
 129        /* partition name
 130         * (16 char in gpt, + 1 for null terminated string
 131         */
 132        char                    name[16 + 1];
 133        u64                     addr;
 134        u64                     size;
 135        enum stm32prog_part_type bin_nb;        /* SSBL repeatition */
 136
 137        /* information on associated device */
 138        struct stm32prog_dev_t  *dev;           /* pointer to device */
 139        s16                     part_id;        /* partition id in device */
 140        int                     alt_id;         /* alt id in usb/dfu */
 141
 142        struct list_head        list;
 143};
 144
 145#define STM32PROG_MAX_DEV 5
 146struct stm32prog_data {
 147        /* Layout information */
 148        int                     dev_nb;         /* device number*/
 149        struct stm32prog_dev_t  dev[STM32PROG_MAX_DEV]; /* array of device */
 150        int                     part_nb;        /* nb of partition */
 151        struct stm32prog_part_t *part_array;    /* array of partition */
 152#ifdef CONFIG_STM32MP15x_STM32IMAGE
 153        bool                    tee_detected;
 154#endif
 155        bool                    fsbl_nor_detected;
 156
 157        /* command internal information */
 158        unsigned int            phase;
 159        u32                     offset;
 160        char                    error[255];
 161        struct stm32prog_part_t *cur_part;
 162        void                    *otp_part;
 163        u8                      pmic_part[PMIC_SIZE];
 164
 165        /* SERIAL information */
 166        u32     cursor;
 167        u32     packet_number;
 168        u8      *buffer; /* size = USART_RAM_BUFFER_SIZE*/
 169        int     dfu_seq;
 170        u8      read_phase;
 171
 172        /* bootm information */
 173        uintptr_t       uimage;
 174        uintptr_t       dtb;
 175        uintptr_t       initrd;
 176        size_t          initrd_size;
 177
 178        uintptr_t       script;
 179
 180        /* OPTEE PTA NVMEM */
 181        struct udevice *tee;
 182        u32 tee_session;
 183};
 184
 185extern struct stm32prog_data *stm32prog_data;
 186
 187/* OTP access */
 188int stm32prog_otp_write(struct stm32prog_data *data, u32 offset,
 189                        u8 *buffer, long *size);
 190int stm32prog_otp_read(struct stm32prog_data *data, u32 offset,
 191                       u8 *buffer, long *size);
 192int stm32prog_otp_start(struct stm32prog_data *data);
 193
 194/* PMIC access */
 195int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset,
 196                         u8 *buffer, long *size);
 197int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset,
 198                        u8 *buffer, long *size);
 199int stm32prog_pmic_start(struct stm32prog_data *data);
 200
 201/* generic part*/
 202void stm32prog_header_check(uintptr_t raw_header, struct image_header_s *header);
 203int stm32prog_dfu_init(struct stm32prog_data *data);
 204void stm32prog_next_phase(struct stm32prog_data *data);
 205void stm32prog_do_reset(struct stm32prog_data *data);
 206
 207char *stm32prog_get_error(struct stm32prog_data *data);
 208
 209#define stm32prog_err(args...) {\
 210        if (data->phase != PHASE_RESET) { \
 211                sprintf(data->error, args); \
 212                data->phase = PHASE_RESET; \
 213                log_err("Error: %s\n", data->error); } \
 214        }
 215
 216/* Main function */
 217int stm32prog_init(struct stm32prog_data *data, uintptr_t addr, ulong size);
 218void stm32prog_clean(struct stm32prog_data *data);
 219
 220#ifdef CONFIG_CMD_STM32PROG_SERIAL
 221int stm32prog_serial_init(struct stm32prog_data *data, int link_dev);
 222bool stm32prog_serial_loop(struct stm32prog_data *data);
 223#else
 224static inline int stm32prog_serial_init(struct stm32prog_data *data, int link_dev)
 225{
 226        return -ENOSYS;
 227}
 228
 229static inline bool stm32prog_serial_loop(struct stm32prog_data *data)
 230{
 231        return false;
 232}
 233#endif
 234
 235#ifdef CONFIG_CMD_STM32PROG_USB
 236bool stm32prog_usb_loop(struct stm32prog_data *data, int dev);
 237#else
 238static inline bool stm32prog_usb_loop(struct stm32prog_data *data, int dev)
 239{
 240        return false;
 241}
 242#endif
 243
 244#endif
 245