SigmaStar的Uboot UFU升级模式

2022-6-21|2026-2-19
Pavel Han
Pavel Han
date
Jun 21, 2022
slug
2022-06-21-sigmastar-uboot-ufu-firmware-upgrade-mode
status
Published
tags
嵌入式
summary
本文总结了SigmaStar在Uboot中增加的UFU模式,来实现使用该方案的情况下,通过Uboot和USB连接来给终端用户提供安全升级的流程
type
Post
category
音视频
AI summary
Sigmastar provides three options of firmware upgrade worflow:
  1. OTA pack and unpack in application layer
  1. Firmware upgrade thread running in UVC sample application
  1. Using USBDownloader tools and uboot UFU mode
But both option 1# and 2# works in Linux application mode, and Because Sigmastar solution don’t have AB partition design, so option 1# and 2# are not secure upgrade workflow. It means if the end user plug out usb connection or power off camera when firmware upgrading in option 1# and 2# mode, it will be bricked.
So if we want to provide a secure OTA upgrade worflow to the end user, we can only use option 3#.

UBoot UFU mode

Actually Option 3# depends heavily on the uboot UFU work mode. so it will implement the flash image burning process in uboot stage.
In the Uboot UFU mode, actually camera device will configure its usb device function to be a gadget mass storage device, so from PC side, it will find a usb mass storage device if camera enter into uboot ufu mode, and then it can send usb scsi command to run script in uboot.
In the uboot bootup flow, it will check a uboot environment varible: ota_upgrade_status.
  • If ota_upgrade_status=0, it will start normal boot, means loading kernel and rootfs image to RAM, and start kernel booting;
  • If ota_upgrade_status=1, it will enter into UFU mode, and wait the UFU connection from PC side(Open USBDownloadTool in PC side). When the UFU connection is ready between the two sides, uboot will start download a auto_reset.txt script file from pc side, and then parse and execute the command line in the script file to complete the whole upgrade workflow.
So the uboot env ota_upgrade_status is very important for this upgrade workflow. And when the upgrade flow complete scuccessfully, we need to set this varible to be 0, or Nexttime the uboot will still enter into UFU mode automatically.
In Sigmastar’s Uboot UFU environment, there are two very important command which will be used in the upgrade process:
  • tftp: download file from UFU PC side directory. The usage of this tftp command is same to the network tftp command, but this UFU tftp use usb connection to download file.
  • estar: using UFU tftp command to download a script(default script name: auto_update.txt) from PC side directory, and run this script in Sigmastar’s Uboot environment.

The Whole Upgrade Flow

notion image

Code Analysis

  1. Setting ota_upgrade_status to be 1 from PC side USBDownloadTool.
    1. In the button clicked callback function of PC side USBDownloadTool source code, we can find:
      • In this button callback function, it will first find SCSI device which means the camera is in USB UFU mode. but currently our camera is runnning in UVC mode, so USB_ScsiFindDevice will return NULL;
      • Next it will use GetVideoDeviceList API to get the USB UVC device list which connected to this PC, and then in IdentifyDevice function, it will use PID and VID to filter out the target UVC device.
        • The default VID is 0x114b or 0x1d6b, and PID is 0x23fb or 0102. so if we changes the USB PID and VID, also need to change the filter condition.
      • If it find target UVC device, it will use AITAPI_OpenDeviceByPath API to open this device, and then use AITAPI_UvcExtSet to write UVC XU command to this UVC device.
  1. After camera recerives the UVC XU command, it will write ota_upgrade_status environment variable to be 1(in internal/uvc/st_uvc_xu.c):
      • After UVC camera recerives this UVC XU command, it will use system API to write ota_upgrade_status to be 1, and then reboot.
  1. In the Uboot workflow, if it checks ota_upgrade_status equals to be 1, it will enter into UFU mode. In the source code of uboot(common/autoboot.c):
      • After Uboot enter into UFU mode, it will wait for the Host side UFU Connection and wait the UFU command from host side.
  1. Also in the button callback function of PC side USBDownloadTool, after it send out UVC XU command, it will also wait the UFU connection completes and send out UFU command:
      • Ater USBDownloadTool send out UVC XU command, it will loop scan SCSI device to detect if the camera has entered into UFU mode. If so, it will create a new AutoUpdateThreadEntry thread to do the fw upgrade task.
      • In the AutoUpdateThreadEntry thread, it will load the auto_update.txt script file, and then parse and execute the upgrade command in this file.
      • Please be Noted: Above autoUpdateThreadEntry is not secure, because it set the ota_upgrade_status variable to be 0 before the upgrade task completes. so if end user disconnect usb connection in the fw upgrading process, it will be bricked and can’t be recovered. More reasonable solution is: You should reset this ota_upgrade_status variable after fw upgrading task completes succesfully, or put the reset operation in the end of the auto_update.txt file.
  1. Following is a auto_update.txt sample:
      • USBDownloadTool will parse this auto_update.txt, and run all the command step by step;
      • Just as expained in the earlier section, estar command is used to download the script file into RAM and run in uboot environment;
      • Please be Noted: DON’T upgrade boot section (include IPL, IPL_CUST, and uboot), or if usb connection disconnects in the process of boot section upgrading, it will be bricked and can’t recover.
  1. Following is a rootfs upgrade script file sample([[rootfs.es) to explain how to upgrade rootfs partition:
      • This code is very simple, which will run in uboot to download rootfs image to RAM, and write to NAND Flash.
UCConfig学习笔记之UCConfig Mode 0团队开发使用SVN的标准流程
Loading...