@@ -17,6 +17,7 @@ package updater
1717
1818import (
1919 "context"
20+ "encoding/hex"
2021 "fmt"
2122 "runtime"
2223 "strings"
@@ -93,7 +94,7 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes
9394}
9495
9596func FlashBoard (ctx context.Context , downloadedImagePath string , version string , upgradeConfirmCb DownloadConfirmCB , forceYes bool , preserveUser bool ) error {
96- if ! forceYes {
97+ if ! forceYes && ! preserveUser {
9798 res , err := upgradeConfirmCb (version )
9899 if err != nil {
99100 return err
@@ -142,7 +143,30 @@ func FlashBoard(ctx context.Context, downloadedImagePath string, version string,
142143
143144 rawProgram := "rawprogram0.xml"
144145 if preserveUser {
145- rawProgram = "rawprogram0.nouser.xml"
146+ if ok , errT := checkBoardGPTTable (ctx , qdlPath , flashDir ); ok && errT == nil {
147+ rawProgram = "rawprogram0.nouser.xml"
148+ } else {
149+ res , err := func (target string ) (bool , error ) {
150+ feedback .Printf ("\n WARNING: %v.\n Flashing a new Linux image on the board will erase any existing data you have on it." , errT )
151+ feedback .Printf ("Do you want to proceed and flash %s on the board? (yes/no)" , target )
152+
153+ var yesInput string
154+ _ , err := fmt .Scanf ("%s\n " , & yesInput )
155+ if err != nil {
156+ return false , err
157+ }
158+ yes := strings .ToLower (yesInput ) == "yes" || strings .ToLower (yesInput ) == "y"
159+ return yes , nil
160+ }(version )
161+ if err != nil {
162+ return err
163+ }
164+ if ! res {
165+ feedback .Print (i18n .Tr ("Flashing not confirmed by user, exiting" ))
166+ return nil
167+ }
168+ }
169+
146170 }
147171
148172 feedback .Print (i18n .Tr ("Flashing with qdl" ))
@@ -162,3 +186,35 @@ func FlashBoard(ctx context.Context, downloadedImagePath string, version string,
162186
163187 return nil
164188}
189+
190+ func checkBoardGPTTable (ctx context.Context , qdlPath , flashDir * paths.Path ) (bool , error ) {
191+ dumpBinPath := qdlPath .Parent ().Join ("dump.bin" )
192+ readXMLPath := qdlPath .Parent ().Join ("read.xml" )
193+ err := readXMLPath .WriteFile (artifacts .ReadXML )
194+ if err != nil {
195+ return false , err
196+ }
197+ cmd , err := paths .NewProcess (nil , qdlPath .String (), "--storage" , "emmc" , flashDir .Join ("prog_firehose_ddr.elf" ).String (), readXMLPath .String ())
198+ if err != nil {
199+ return false , err
200+ }
201+ cmd .SetDir (qdlPath .Parent ().String ())
202+ if err := cmd .RunWithinContext (ctx ); err != nil {
203+ return false , err
204+ }
205+ if ! dumpBinPath .Exist () {
206+ return false , fmt .Errorf ("it was not possible to access the current Debian image GPT table" )
207+ }
208+ dump , err := dumpBinPath .ReadFile ()
209+ if err != nil {
210+ return false , err
211+ }
212+ strDump := hex .Dump (dump )
213+
214+ if strings .Contains (strDump , "00000250 4c 00 00 00" ) {
215+ fmt .Println ("R0" )
216+ return false , fmt .Errorf ("the current Debian image (R0) does not support user partition preservation" )
217+ }
218+
219+ return true , nil
220+ }
0 commit comments