228a25d37f3f4c8a0ea640fc412fab36c90d3693
[oweals/u-boot.git] / arch / arm / mach-stm32mp / cmd_stm32prog / stm32prog.h
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_END               0xFE
15 #define PHASE_RESET             0xFF
16 #define PHASE_DO_RESET          0x1FF
17
18 #define DEFAULT_ADDRESS         0xFFFFFFFF
19
20 enum stm32prog_target {
21         STM32PROG_NONE,
22         STM32PROG_MMC,
23 };
24
25 enum stm32prog_link_t {
26         LINK_USB,
27         LINK_UNDEFINED,
28 };
29
30 struct image_header_s {
31         bool    present;
32         u32     image_checksum;
33         u32     image_length;
34 };
35
36 struct raw_header_s {
37         u32 magic_number;
38         u32 image_signature[64 / 4];
39         u32 image_checksum;
40         u32 header_version;
41         u32 image_length;
42         u32 image_entry_point;
43         u32 reserved1;
44         u32 load_address;
45         u32 reserved2;
46         u32 version_number;
47         u32 option_flags;
48         u32 ecdsa_algorithm;
49         u32 ecdsa_public_key[64 / 4];
50         u32 padding[83 / 4];
51         u32 binary_type;
52 };
53
54 #define BL_HEADER_SIZE  sizeof(struct raw_header_s)
55
56 /* partition type in flashlayout file */
57 enum stm32prog_part_type {
58         PART_BINARY,
59         PART_SYSTEM,
60         PART_FILESYSTEM,
61         RAW_IMAGE
62 };
63
64 /* device information */
65 struct stm32prog_dev_t {
66         enum stm32prog_target   target;
67         char                    dev_id;
68         u32                     erase_size;
69         struct mmc              *mmc;
70         /* list of partition for this device / ordered in offset */
71         struct list_head        part_list;
72 };
73
74 /* partition information build from FlashLayout and device */
75 struct stm32prog_part_t {
76         /* FlashLayout information */
77         int                     option;
78         int                     id;
79         enum stm32prog_part_type part_type;
80         enum stm32prog_target   target;
81         char                    dev_id;
82
83         /* partition name
84          * (16 char in gpt, + 1 for null terminated string
85          */
86         char                    name[16 + 1];
87         u64                     addr;
88         u64                     size;
89
90         /* information on associated device */
91         struct stm32prog_dev_t  *dev;           /* pointer to device */
92         u16                     part_id;        /* partition id in device */
93         int                     alt_id;         /* alt id in usb/dfu */
94
95         struct list_head        list;
96 };
97
98 #define STM32PROG_MAX_DEV 5
99 struct stm32prog_data {
100         /* Layout information */
101         int                     dev_nb;         /* device number*/
102         struct stm32prog_dev_t  dev[STM32PROG_MAX_DEV]; /* array of device */
103         int                     part_nb;        /* nb of partition */
104         struct stm32prog_part_t *part_array;    /* array of partition */
105
106         /* command internal information */
107         unsigned int            phase;
108         u32                     offset;
109         char                    error[255];
110         struct stm32prog_part_t *cur_part;
111
112         /* STM32 header information */
113         struct raw_header_s     *header_data;
114         struct image_header_s   header;
115 };
116
117 extern struct stm32prog_data *stm32prog_data;
118
119 /* generic part*/
120 u8 stm32prog_header_check(struct raw_header_s *raw_header,
121                           struct image_header_s *header);
122 int stm32prog_dfu_init(struct stm32prog_data *data);
123 void stm32prog_next_phase(struct stm32prog_data *data);
124 void stm32prog_do_reset(struct stm32prog_data *data);
125
126 char *stm32prog_get_error(struct stm32prog_data *data);
127
128 #define stm32prog_err(args...) {\
129         if (data->phase != PHASE_RESET) { \
130                 sprintf(data->error, args); \
131                 data->phase = PHASE_RESET; \
132                 pr_err("Error: %s\n", data->error); } \
133         }
134
135 /* Main function */
136 int stm32prog_init(struct stm32prog_data *data, ulong addr, ulong size);
137 bool stm32prog_usb_loop(struct stm32prog_data *data, int dev);
138 void stm32prog_clean(struct stm32prog_data *data);
139
140 #endif