1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
13 #include <dm/device-internal.h>
14 #include "stm32prog.h"
16 /* - configuration part -----------------------------*/
17 #define USART_BL_VERSION 0x40 /* USART bootloader version V4.0*/
18 #define UBOOT_BL_VERSION 0x03 /* bootloader version V0.3*/
19 #define DEVICE_ID_BYTE1 0x05 /* MSB byte of device ID*/
20 #define DEVICE_ID_BYTE2 0x00 /* LSB byte of device ID*/
21 #define USART_RAM_BUFFER_SIZE 256 /* Size of USART_RAM_Buf buffer*/
23 /* - Commands -----------------------------*/
24 #define GET_CMD_COMMAND 0x00 /* Get CMD command*/
25 #define GET_VER_COMMAND 0x01 /* Get Version command*/
26 #define GET_ID_COMMAND 0x02 /* Get ID command*/
27 #define GET_PHASE_COMMAND 0x03 /* Get Phase command*/
28 #define RM_COMMAND 0x11 /* Read Memory command*/
29 #define READ_PART_COMMAND 0x12 /* Read Partition command*/
30 #define START_COMMAND 0x21 /* START command (Go)*/
31 #define DOWNLOAD_COMMAND 0x31 /* Download command*/
32 /* existing command for other STM32 but not used */
34 /* EXTENDED_ERASE 0x44 */
35 /* WRITE_UNPROTECTED 0x73 */
36 /* READOUT_PROTECT 0x82 */
37 /* READOUT_UNPROTECT 0x92 */
39 /* - miscellaneous defines ----------------------------------------*/
40 #define INIT_BYTE 0x7F /*Init Byte ID*/
41 #define ACK_BYTE 0x79 /*Acknowlede Byte ID*/
42 #define NACK_BYTE 0x1F /*No Acknowlede Byte ID*/
43 #define ABORT_BYTE 0x5F /*ABORT*/
45 struct udevice *down_serial_dev;
58 #define NB_CMD sizeof(cmd_id)
60 /* DFU support for serial *********************************************/
61 static struct dfu_entity *stm32prog_get_entity(struct stm32prog_data *data)
66 if (data->phase == PHASE_FLASHLAYOUT)
71 alt_id = data->cur_part->alt_id;
73 return dfu_get_entity(alt_id);
76 static int stm32prog_write(struct stm32prog_data *data, u8 *buffer,
79 struct dfu_entity *dfu_entity;
82 dfu_entity = stm32prog_get_entity(data);
86 ret = dfu_write(dfu_entity,
92 stm32prog_err("DFU write failed [%d] cnt: %d",
96 /* handle rollover as in driver/dfu/dfu.c */
97 data->dfu_seq &= 0xffff;
99 data->dfu_seq = 0; /* flush done */
104 static int stm32prog_read(struct stm32prog_data *data, u8 phase, u32 offset,
105 u8 *buffer, u32 buffer_size)
107 struct dfu_entity *dfu_entity;
108 struct stm32prog_part_t *part;
113 stm32prog_err("DFU write pending for phase %d, seq %d",
114 data->phase, data->dfu_seq);
117 if (phase == PHASE_FLASHLAYOUT || phase > PHASE_LAST_USER) {
118 stm32prog_err("read failed : phase %d is invalid", phase);
121 if (data->read_phase <= PHASE_LAST_USER &&
122 phase != data->read_phase) {
123 /* clear previous read session */
124 dfu_entity = dfu_get_entity(data->read_phase - 1);
126 dfu_transaction_cleanup(dfu_entity);
130 /* found partition for the expected phase */
131 for (i = 0; i < data->part_nb; i++) {
132 part = &data->part_array[i];
133 if (part->id == phase)
134 dfu_entity = dfu_get_entity(part->alt_id);
137 stm32prog_err("read failed : phase %d is unknown", phase);
141 /* clear pending read before to force offset */
142 if (dfu_entity->inited &&
143 (data->read_phase != phase || data->offset != offset))
144 dfu_transaction_cleanup(dfu_entity);
146 /* initiate before to force offset */
147 if (!dfu_entity->inited) {
148 ret = dfu_transaction_initiate(dfu_entity, true);
150 stm32prog_err("DFU read init failed [%d] phase = %d offset = 0x%08x",
155 /* force new offset */
156 if (dfu_entity->offset != offset)
157 dfu_entity->offset = offset;
158 data->offset = offset;
159 data->read_phase = phase;
160 pr_debug("\nSTM32 download read %s offset=0x%x\n",
161 dfu_entity->name, offset);
162 ret = dfu_read(dfu_entity, buffer, buffer_size,
163 dfu_entity->i_blk_seq_num);
165 stm32prog_err("DFU read failed [%d] phase = %d offset = 0x%08x",
172 if (size < buffer_size) {
174 data->read_phase = PHASE_END;
175 memset(buffer + size, 0, buffer_size - size);
177 data->offset += size;
183 /* UART access ***************************************************/
184 int stm32prog_serial_init(struct stm32prog_data *data, int link_dev)
186 struct udevice *dev = NULL;
190 struct dm_serial_ops *ops;
191 /* no parity, 8 bits, 1 stop */
192 u32 serial_config = SERIAL_DEFAULT_CONFIG;
194 down_serial_dev = NULL;
196 sprintf(alias, "serial%d", link_dev);
197 path = fdt_get_alias(gd->fdt_blob, alias);
199 pr_err("%s alias not found", alias);
202 node = fdt_path_offset(gd->fdt_blob, path);
203 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node,
205 down_serial_dev = dev;
206 } else if (node > 0 &&
207 !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
209 if (!device_probe(dev))
210 down_serial_dev = dev;
212 if (!down_serial_dev) {
213 pr_err("%s = %s device not found", alias, path);
217 /* force silent console on uart only when used */
218 if (gd->cur_serial_dev == down_serial_dev)
219 gd->flags |= GD_FLG_DISABLE_CONSOLE | GD_FLG_SILENT;
221 gd->flags &= ~(GD_FLG_DISABLE_CONSOLE | GD_FLG_SILENT);
223 ops = serial_get_ops(down_serial_dev);
226 pr_err("%s = %s missing ops", alias, path);
229 if (!ops->setconfig) {
230 pr_err("%s = %s missing setconfig", alias, path);
234 clrsetbits_le32(&serial_config, SERIAL_PAR_MASK, SERIAL_PAR_EVEN);
236 data->buffer = memalign(CONFIG_SYS_CACHELINE_SIZE,
237 USART_RAM_BUFFER_SIZE);
239 return ops->setconfig(down_serial_dev, serial_config);
242 static void stm32prog_serial_flush(void)
244 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
248 err = ops->getc(down_serial_dev);
249 } while (err != -EAGAIN);
252 static int stm32prog_serial_getc_err(void)
254 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
258 err = ops->getc(down_serial_dev);
259 if (err == -EAGAIN) {
263 } while ((err == -EAGAIN) && (!had_ctrlc()));
268 static u8 stm32prog_serial_getc(void)
272 err = stm32prog_serial_getc_err();
274 return err >= 0 ? err : 0;
277 static bool stm32prog_serial_get_buffer(u8 *buffer, u32 *count)
279 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
283 err = ops->getc(down_serial_dev);
287 } else if (err == -EAGAIN) {
293 } while (*count && !had_ctrlc());
298 static void stm32prog_serial_putc(u8 w_byte)
300 struct dm_serial_ops *ops = serial_get_ops(down_serial_dev);
304 err = ops->putc(down_serial_dev, w_byte);
305 } while (err == -EAGAIN);
308 /* Helper function ************************************************/
310 static u8 stm32prog_header(struct stm32prog_data *data)
314 struct dfu_entity *dfu_entity;
317 dfu_entity = stm32prog_get_entity(data);
321 printf("\nSTM32 download write %s\n", dfu_entity->name);
323 /* force cleanup to avoid issue with previous read */
324 dfu_transaction_cleanup(dfu_entity);
326 ret = stm32prog_header_check(data->header_data,
329 /* no header : max size is partition size */
331 dfu_entity->get_medium_size(dfu_entity, &size);
332 data->header.image_length = size;
335 /**** Flash the header if necessary for boot partition */
336 if (data->phase < PHASE_FIRST_USER)
339 /* write header if boot partition */
342 stm32prog_err("invalid header (error %d)", ret);
344 ret = stm32prog_write(data,
345 (u8 *)data->header_data,
350 printf(" partition without checksum\n");
354 free(data->header_data);
355 data->header_data = NULL;
360 static u8 stm32prog_start(struct stm32prog_data *data, u32 address)
363 struct dfu_entity *dfu_entity;
365 if (address < 0x100) {
366 if (address == PHASE_OTP)
367 return stm32prog_otp_start(data);
369 if (address == PHASE_PMIC)
370 return stm32prog_pmic_start(data);
372 if (address == PHASE_RESET || address == PHASE_END) {
373 data->cur_part = NULL;
375 data->phase = address;
378 if (address != data->phase) {
379 stm32prog_err("invalid received phase id %d, current phase is %d",
380 (u8)address, (u8)data->phase);
384 /* check the last loaded partition */
385 if (address == DEFAULT_ADDRESS || address == data->phase) {
386 switch (data->phase) {
390 data->cur_part = NULL;
391 data->phase = PHASE_DO_RESET;
394 dfu_entity = stm32prog_get_entity(data);
399 ret = dfu_flush(dfu_entity, NULL, 0, data->dfu_seq);
402 stm32prog_err("DFU flush failed [%d]", ret);
406 printf("\n received length = 0x%x\n", data->cursor);
407 if (data->header.present) {
409 (data->header.image_length + BL_HEADER_SIZE)) {
410 stm32prog_err("transmission interrupted (length=0x%x expected=0x%x)",
412 data->header.image_length +
416 if (data->header.image_checksum != data->checksum) {
417 stm32prog_err("invalid checksum received (0x%x expected 0x%x)",
419 data->header.image_checksum);
422 printf("\n checksum OK (0x%x)\n", data->checksum);
425 /* update DFU with received flashlayout */
426 if (data->phase == PHASE_FLASHLAYOUT)
427 stm32prog_dfu_init(data);
429 void (*entry)(void) = (void *)address;
431 printf("## Starting application at 0x%x ...\n", address);
433 printf("## Application terminated\n");
441 * get_address() - Get address if it is valid
443 * @tmp_xor: Current xor value to update
444 * @return The address area
446 static u32 get_address(u8 *tmp_xor)
451 data = stm32prog_serial_getc();
453 address |= ((u32)data) << 24;
455 data = stm32prog_serial_getc();
456 address |= ((u32)data) << 16;
459 data = stm32prog_serial_getc();
460 address |= ((u32)data) << 8;
463 data = stm32prog_serial_getc();
464 address |= ((u32)data);
470 static void stm32prog_serial_result(u8 result)
472 /* always flush fifo before to send result */
473 stm32prog_serial_flush();
474 stm32prog_serial_putc(result);
477 /* Command -----------------------------------------------*/
479 * get_cmd_command() - Respond to Get command
481 * @data: Current command context
483 static void get_cmd_command(struct stm32prog_data *data)
487 stm32prog_serial_putc(NB_CMD);
488 stm32prog_serial_putc(USART_BL_VERSION);
490 for (counter = 0; counter < NB_CMD; counter++)
491 stm32prog_serial_putc(cmd_id[counter]);
493 stm32prog_serial_result(ACK_BYTE);
497 * get_version_command() - Respond to Get Version command
499 * @data: Current command context
501 static void get_version_command(struct stm32prog_data *data)
503 stm32prog_serial_putc(UBOOT_BL_VERSION);
504 stm32prog_serial_result(ACK_BYTE);
508 * get_id_command() - Respond to Get ID command
510 * @data: Current command context
512 static void get_id_command(struct stm32prog_data *data)
514 /* Send Device IDCode */
515 stm32prog_serial_putc(0x1);
516 stm32prog_serial_putc(DEVICE_ID_BYTE1);
517 stm32prog_serial_putc(DEVICE_ID_BYTE2);
518 stm32prog_serial_result(ACK_BYTE);
522 * get_phase_command() - Respond to Get phase
524 * @data: Current command context
526 static void get_phase_command(struct stm32prog_data *data)
528 char *err_msg = NULL;
530 u32 destination = DEFAULT_ADDRESS; /* destination address */
531 int phase = data->phase;
533 if (phase == PHASE_RESET || phase == PHASE_DO_RESET) {
534 err_msg = stm32prog_get_error(data);
535 length = strlen(err_msg);
537 if (phase == PHASE_FLASHLAYOUT)
538 destination = STM32_DDR_BASE;
540 stm32prog_serial_putc(length + 5); /* Total length */
541 stm32prog_serial_putc(phase & 0xFF); /* partition ID */
542 stm32prog_serial_putc(destination); /* byte 1 of address */
543 stm32prog_serial_putc(destination >> 8); /* byte 2 of address */
544 stm32prog_serial_putc(destination >> 16); /* byte 3 of address */
545 stm32prog_serial_putc(destination >> 24); /* byte 4 of address */
547 stm32prog_serial_putc(length); /* Information length */
548 for (i = 0; i < length; i++)
549 stm32prog_serial_putc(err_msg[i]);
550 stm32prog_serial_result(ACK_BYTE);
552 if (phase == PHASE_RESET)
553 stm32prog_do_reset(data);
557 * read_memory_command() - Read data from memory
559 * @data: Current command context
561 static void read_memory_command(struct stm32prog_data *data)
564 u8 rcv_data = 0x0, tmp_xor = 0x0;
567 /* Read memory address */
568 address = get_address(&tmp_xor);
570 /* If address memory is not received correctly */
571 rcv_data = stm32prog_serial_getc();
572 if (rcv_data != tmp_xor) {
573 stm32prog_serial_result(NACK_BYTE);
577 stm32prog_serial_result(ACK_BYTE);
579 /* Read the number of bytes to be received:
580 * Max NbrOfData = Data + 1 = 256
582 rcv_data = stm32prog_serial_getc();
584 if (stm32prog_serial_getc() != tmp_xor) {
585 stm32prog_serial_result(NACK_BYTE);
589 /* If checksum is correct send ACK */
590 stm32prog_serial_result(ACK_BYTE);
592 /* Send data to the host:
593 * Number of data to read = data + 1
595 for (counter = (rcv_data + 1); counter != 0; counter--)
596 stm32prog_serial_putc(*(u8 *)(address++));
600 * start_command() - Respond to start command
602 * Jump to user application in RAM or partition check
604 * @data: Current command context
606 static void start_command(struct stm32prog_data *data)
612 /* Read memory address */
613 address = get_address(&tmp_xor);
615 /* If address memory is not received correctly */
616 rcv_data = stm32prog_serial_getc();
617 if (rcv_data != tmp_xor) {
618 stm32prog_serial_result(NACK_BYTE);
621 /* validate partition */
622 ret = stm32prog_start(data,
626 stm32prog_serial_result(ABORT_BYTE);
628 stm32prog_serial_result(ACK_BYTE);
632 * download_command() - Respond to download command
634 * Write data to not volatile memory, Flash
636 * @data: Current command context
638 static void download_command(struct stm32prog_data *data)
643 u32 counter = 0x0, codesize = 0x0;
646 struct image_header_s *image_header = &data->header;
647 u32 cursor = data->cursor;
651 u32 result = ACK_BYTE;
657 address = get_address(&my_xor);
659 /* If address memory is not received correctly */
660 rcv_xor = stm32prog_serial_getc();
661 if (rcv_xor != my_xor) {
666 /* If address valid send ACK */
667 stm32prog_serial_result(ACK_BYTE);
669 /* get packet number and operation type */
670 operation = (u8)((u32)address >> 24);
671 packet_number = ((u32)(((u32)address << 8))) >> 8;
674 /* supported operation */
675 case PHASE_FLASHLAYOUT:
683 /* check the packet number */
684 if (packet_number == 0) {
685 /* erase: re-initialize the image_header struct */
686 data->packet_number = 0;
687 if (data->header_data)
688 memset(data->header_data, 0, BL_HEADER_SIZE);
690 data->header_data = calloc(1, BL_HEADER_SIZE);
696 data->packet_number++;
699 /* Check with the number of current packet if the device receive
702 if (packet_number != data->packet_number) {
703 data->packet_number--;
708 /*-- Read number of bytes to be written and data -----------*/
710 /* Read the number of bytes to be written:
711 * Max NbrOfData = data + 1 <= 256
713 rcv_data = stm32prog_serial_getc();
715 /* NbrOfData to write = data + 1 */
716 codesize = rcv_data + 0x01;
718 if (codesize > USART_RAM_BUFFER_SIZE) {
723 /* Checksum Initialization */
726 /* UART receive data and send to Buffer */
728 error = stm32prog_serial_get_buffer(data->buffer, &counter);
732 rcv = stm32prog_serial_getc_err();
738 printf("transmission error on packet %d, byte %d\n",
739 packet_number, codesize - counter);
740 /* waiting end of packet before flush & NACK */
742 data->packet_number--;
747 /* Compute Checksum */
748 ramaddress = data->buffer;
749 for (counter = codesize; counter != 0; counter--)
750 my_xor ^= *(ramaddress++);
752 /* If Checksum is incorrect */
753 if (rcv_xor != my_xor) {
754 printf("checksum error on packet %d\n",
756 /* wait to be sure that all data are received
757 * in the FIFO before flush
760 data->packet_number--;
765 /* Update current position in buffer */
766 data->cursor += codesize;
768 if (operation == PHASE_OTP) {
769 size = data->cursor - cursor;
770 /* no header for OTP */
771 if (stm32prog_otp_write(data, cursor,
772 data->buffer, &size))
777 if (operation == PHASE_PMIC) {
778 size = data->cursor - cursor;
779 /* no header for PMIC */
780 if (stm32prog_pmic_write(data, cursor,
781 data->buffer, &size))
786 if (cursor < BL_HEADER_SIZE) {
787 /* size = portion of header in this chunck */
788 if (data->cursor >= BL_HEADER_SIZE)
789 size = BL_HEADER_SIZE - cursor;
791 size = data->cursor - cursor;
792 memcpy((void *)((u32)(data->header_data) + cursor),
796 if (cursor == BL_HEADER_SIZE) {
797 /* Check and Write the header */
798 if (stm32prog_header(data)) {
807 if (image_header->present) {
808 if (data->cursor <= BL_HEADER_SIZE)
810 /* compute checksum on payload */
811 for (i = (unsigned long)size; i < codesize; i++)
812 data->checksum += data->buffer[i];
815 image_header->image_length + BL_HEADER_SIZE) {
816 pr_err("expected size exceeded\n");
821 /* write data (payload) */
822 ret = stm32prog_write(data,
827 ret = stm32prog_write(data,
835 stm32prog_serial_result(result);
839 * read_partition() - Respond to read command
841 * Read data from not volatile memory, Flash
843 * @data: Current command context
845 static void read_partition_command(struct stm32prog_data *data)
847 u32 i, part_id, codesize, offset = 0, rcv_data;
853 part_id = stm32prog_serial_getc();
856 offset = get_address(&tmp_xor);
858 rcv_data = stm32prog_serial_getc();
859 if (rcv_data != tmp_xor) {
860 pr_debug("1st checksum received = %x, computed %x\n",
864 stm32prog_serial_putc(ACK_BYTE);
866 /* NbrOfData to read = data + 1 */
867 rcv_data = stm32prog_serial_getc();
868 codesize = rcv_data + 0x01;
871 rcv_data = stm32prog_serial_getc();
872 if ((rcv_data ^ tmp_xor) != 0xFF) {
873 pr_debug("2nd checksum received = %x, computed %x\n",
878 pr_debug("%s : %x\n", __func__, part_id);
883 if (!stm32prog_otp_read(data, offset, buffer, &size))
888 if (!stm32prog_pmic_read(data, offset, buffer, &size))
892 res = stm32prog_read(data, part_id, offset,
899 stm32prog_serial_putc(ACK_BYTE);
900 /*----------- Send data to the host -----------*/
901 for (i = 0; i < rcv_data; i++)
902 stm32prog_serial_putc(buffer[i]);
903 /*----------- Send filler to the host -----------*/
904 for (; i < codesize; i++)
905 stm32prog_serial_putc(0x0);
908 stm32prog_serial_result(ABORT_BYTE);
912 stm32prog_serial_result(NACK_BYTE);
915 /* MAIN function = SERIAL LOOP ***********************************************/
918 * stm32prog_serial_loop() - USART bootloader Loop routine
920 * @data: Current command context
921 * @return true if reset is needed after loop
923 bool stm32prog_serial_loop(struct stm32prog_data *data)
928 int phase = data->phase;
930 /* element of cmd_func need to aligned with cmd_id[]*/
931 void (*cmd_func[NB_CMD])(struct stm32prog_data *) = {
932 /* GET_CMD_COMMAND */ get_cmd_command,
933 /* GET_VER_COMMAND */ get_version_command,
934 /* GET_ID_COMMAND */ get_id_command,
935 /* GET_PHASE_COMMAND */ get_phase_command,
936 /* RM_COMMAND */ read_memory_command,
937 /* READ_PART_COMMAND */ read_partition_command,
938 /* START_COMMAND */ start_command,
939 /* DOWNLOAD_COMMAND */ download_command
942 /* flush and NACK pending command received during u-boot init
943 * request command reemit
945 stm32prog_serial_result(NACK_BYTE);
947 clear_ctrlc(); /* forget any previous Control C */
948 while (!had_ctrlc()) {
951 if (phase == PHASE_DO_RESET)
954 /* Get the user command: read first byte */
955 command = stm32prog_serial_getc();
957 if (command == INIT_BYTE) {
958 puts("\nConnected\n");
959 stm32prog_serial_result(ACK_BYTE);
964 for (counter = 0; counter < NB_CMD; counter++)
965 if (cmd_id[counter] == command) {
970 if ((command ^ stm32prog_serial_getc()) != 0xFF)
973 /* wait to be sure that all data are received
974 * in the FIFO before flush (CMD and XOR)
977 stm32prog_serial_result(NACK_BYTE);
979 stm32prog_serial_result(ACK_BYTE);
980 cmd_func[counter](data);
986 if (gd->cur_serial_dev == down_serial_dev) {
987 /* restore console on uart */
988 gd->flags &= ~(GD_FLG_DISABLE_CONSOLE | GD_FLG_SILENT);
990 down_serial_dev = NULL;
992 return false; /* no reset after ctrlc */