Skip to content

Commit 7c6ea0c

Browse files
Flasher: add support for flashing non-latest images
1 parent 18f1c7c commit 7c6ea0c

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

updater/download_image.go

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@ import (
77
"encoding/hex"
88
"fmt"
99
"io"
10-
"log/slog"
1110

1211
"github.com/arduino/go-paths-helper"
1312
"github.com/bcmi-labs/orchestrator/cmd/feedback"
13+
"github.com/bcmi-labs/orchestrator/cmd/i18n"
1414
"github.com/codeclysm/extract/v4"
1515
)
1616

17-
// TODO: add more fields to download other image versions
1817
type Manifest struct {
19-
Latest struct {
20-
Version string `json:"version"`
21-
Url string `json:"url"`
22-
Sha256 string `json:"sha256"`
23-
} `json:"latest"`
18+
Latest Release `json:"latest"`
19+
Releases []Release `json:"releases"`
20+
}
21+
22+
type Release struct {
23+
Version string `json:"version"`
24+
Url string `json:"url"`
25+
Sha256 string `json:"sha256"`
2426
}
2527

2628
// DownloadConfirmCB is a function that is called when a Debian image is ready to be downloaded.
@@ -59,6 +61,11 @@ func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb D
5961
return nil, fmt.Errorf("error downloading the image: %v", err)
6062
}
6163

64+
// Download not confirmed
65+
if tmpZip == nil {
66+
return nil, nil
67+
}
68+
6269
err = ExtractImage(tmpZip, tmpZip.Parent())
6370
if err != nil {
6471
return nil, fmt.Errorf("error extracting the image: %v", err)
@@ -71,39 +78,43 @@ func DownloadAndExtract(client *Client, targetVersion string, upgradeConfirmCb D
7178
func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb DownloadConfirmCB, forceYes bool) (*paths.Path, string, error) {
7279
var err error
7380

74-
slog.Info("Checking for Debian image releases")
81+
feedback.Print(i18n.Tr("Checking for Debian image releases"))
7582
manifest, err := client.GetInfoManifest()
7683
if err != nil {
7784
return nil, "", err
7885
}
7986

80-
if targetVersion == "latest" {
81-
targetVersion = manifest.Latest.Version
87+
var rel *Release
88+
if targetVersion == "latest" || targetVersion == manifest.Latest.Version {
89+
rel = &manifest.Latest
90+
} else {
91+
for _, r := range manifest.Releases {
92+
if targetVersion == r.Version {
93+
rel = &r
94+
break
95+
}
96+
}
97+
}
98+
99+
if rel == nil {
100+
return nil, "", fmt.Errorf("could not find Debian image %s", targetVersion)
82101
}
83102

84103
if !forceYes {
85-
res, err := upgradeConfirmCb(targetVersion)
104+
res, err := upgradeConfirmCb(rel.Version)
86105
if err != nil {
87106
return nil, "", err
88107
}
89108
if !res {
90-
slog.Info("Download not confirmed by user, exiting")
109+
feedback.Print(i18n.Tr("Download not confirmed by user, exiting"))
91110
return nil, "", nil
92111
}
93112
}
94113

95-
// Download the Debian image
96-
var download io.ReadCloser
97-
var size int64
98-
if targetVersion == manifest.Latest.Version {
99-
slog.Info("Downloading Debian image", "version", manifest.Latest.Version)
100-
download, size, err = client.FetchZip(manifest.Latest.Url)
101-
if err != nil {
102-
return nil, "", fmt.Errorf("could not fetch Debian image: %w", err)
103-
}
104-
} else {
105-
// TODO: check the json for the specific version and download it
106-
return nil, "", nil
114+
feedback.Print(i18n.Tr("Downloading Debian image version %s", rel.Version))
115+
download, size, err := client.FetchZip(rel.Url)
116+
if err != nil {
117+
return nil, "", fmt.Errorf("could not fetch Debian image: %w", err)
107118
}
108119
defer download.Close()
109120

@@ -128,20 +139,20 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
128139
}
129140

130141
// Check the hash
131-
if sha256Byte, err := hex.DecodeString(manifest.Latest.Sha256); err != nil {
142+
if sha256Byte, err := hex.DecodeString(rel.Sha256); err != nil {
132143
return nil, "", fmt.Errorf("could not convert sha256 from hex to bytes: %w", err)
133144
} else if s := checksum.Sum(nil); !bytes.Equal(s, sha256Byte) {
134145
return nil, "", fmt.Errorf("bad hash: %x (expected %x)", s, sha256Byte)
135146
}
136147

137-
slog.Info("Download of Debian image completed", "path", temp)
148+
feedback.Print(i18n.Tr("Download of Debian image completed"))
138149

139-
return tmpZip, targetVersion, nil
150+
return tmpZip, rel.Version, nil
140151
}
141152

142153
func ExtractImage(archive, temp *paths.Path) error {
143154
// Unzip the Debian image
144-
slog.Info("Unzipping Debian image", "tmpDir", temp)
155+
feedback.Print(i18n.Tr("Unzipping Debian image"))
145156
tmpZipFile, err := archive.Open()
146157
if err != nil {
147158
return fmt.Errorf("could not open archive: %w", err)

updater/flasher.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package updater
33
import (
44
"context"
55
"fmt"
6-
"log/slog"
76
"net/url"
87
"runtime"
98
"strings"
@@ -12,6 +11,7 @@ import (
1211

1312
"github.com/arduino/go-paths-helper"
1413
"github.com/bcmi-labs/orchestrator/cmd/feedback"
14+
"github.com/bcmi-labs/orchestrator/cmd/i18n"
1515
)
1616

1717
//go:embed assets
@@ -45,6 +45,11 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes
4545
return fmt.Errorf("could not download and extract the image: %v", err)
4646
}
4747

48+
// Download not confirmed
49+
if tempImagePath == nil {
50+
return nil
51+
}
52+
4853
defer tempImagePath.Parent().RemoveAll()
4954

5055
imagePath = tempImagePath
@@ -113,7 +118,7 @@ func FlashBoard(ctx context.Context, downloadedImagePath string) error {
113118
return err
114119
}
115120
// TODO: add logic to preserve the user partition
116-
slog.Info("Flashing with qdl")
121+
feedback.Print(i18n.Tr("Flashing with qdl"))
117122
cmd, err := paths.NewProcess(nil, qdlPath.String(), "--allow-missing", "--storage", "emmc", "prog_firehose_ddr.elf", "rawprogram0.xml", "patch0.xml")
118123
if err != nil {
119124
return err

0 commit comments

Comments
 (0)