Skip to content

Commit 24f234d

Browse files
Check partition table of the current board image
1 parent 43c911c commit 24f234d

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This file is part of arduino-flasher-cli.
2+
//
3+
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-flasher-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package artifacts
17+
18+
import (
19+
_ "embed"
20+
)
21+
22+
//go:embed read.xml
23+
var ReadXML []byte

updater/artifacts/read.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" ?>
2+
<data>
3+
<read SECTOR_SIZE_IN_BYTES="512" filename="dump.bin" physical_partition_number="0" num_partition_sectors="34" start_sector="0"/>
4+
</data>

updater/flasher.go

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package updater
1717

1818
import (
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

9596
func 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("\nWARNING: %v.\nFlashing 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

Comments
 (0)