@@ -18,11 +18,11 @@ import (
1818
1919const (
2020 imageDirectory = "/data"
21- imageNone = "/dev/mmcblk0p3"
2221 cdromFlag = "/sys/kernel/config/usb_gadget/g0/functions/mass_storage.disk0/lun.0/cdrom"
2322 mountDevice = "/sys/kernel/config/usb_gadget/g0/functions/mass_storage.disk0/lun.0/file"
2423 inquiryString = "/sys/kernel/config/usb_gadget/g0/functions/mass_storage.disk0/lun.0/inquiry_string"
2524 roFlag = "/sys/kernel/config/usb_gadget/g0/functions/mass_storage.disk0/lun.0/ro"
25+ usbNoRstMarker = "/boot/usb.norst"
2626)
2727
2828func (s * Service ) GetImages (c * gin.Context ) {
@@ -108,16 +108,20 @@ func (s *Service) MountImage(c *gin.Context) {
108108 return
109109 }
110110
111- // mount
111+ // mount if file provided
112112 image := req .File
113- if image == "" {
114- image = imageNone
113+ if image != "" {
114+ if err := os .WriteFile (mountDevice , []byte (image ), 0o666 ); err != nil {
115+ log .Errorf ("mount file %s failed: %s" , image , err )
116+ rsp .ErrRsp (c , - 2 , "mount image failed" )
117+ return
118+ }
115119 }
116120
117- if err := os . WriteFile ( mountDevice , [] byte ( image ), 0o666 ); err != nil {
118- log . Errorf ( "mount file %s failed: %s" , image , err )
119- rsp . ErrRsp ( c , - 2 , "mount image failed" )
120- return
121+ // check to see if usb gadget reset is disabled
122+ resetUsb := true
123+ if _ , err := os . Stat ( usbNoRstMarker ); err == nil {
124+ resetUsb = false
121125 }
122126
123127 h := hid .GetHid ()
@@ -129,18 +133,20 @@ func (s *Service) MountImage(c *gin.Context) {
129133 }()
130134
131135 // reset usb
132- commands := []string {
133- "echo > /sys/kernel/config/usb_gadget/g0/UDC" ,
134- "ls /sys/class/udc/ | cat > /sys/kernel/config/usb_gadget/g0/UDC" ,
135- }
136+ if resetUsb {
137+ commands := []string {
138+ "echo > /sys/kernel/config/usb_gadget/g0/UDC" ,
139+ "ls /sys/class/udc/ | cat > /sys/kernel/config/usb_gadget/g0/UDC" ,
140+ }
136141
137- for _ , command := range commands {
138- err := exec .Command ("sh" , "-c" , command ).Run ()
139- if err != nil {
140- rsp .ErrRsp (c , - 2 , "execute command failed" )
141- return
142+ for _ , command := range commands {
143+ err := exec .Command ("sh" , "-c" , command ).Run ()
144+ if err != nil {
145+ rsp .ErrRsp (c , - 2 , "execute command failed" )
146+ return
147+ }
148+ time .Sleep (100 * time .Millisecond )
142149 }
143- time .Sleep (100 * time .Millisecond )
144150 }
145151
146152 rsp .OkRsp (c )
@@ -157,9 +163,6 @@ func (s *Service) GetMountedImage(c *gin.Context) {
157163 }
158164
159165 image := strings .ReplaceAll (string (content ), "\n " , "" )
160- if image == imageNone {
161- image = ""
162- }
163166
164167 data := & proto.GetMountedImageRsp {
165168 File : image ,
0 commit comments