Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src-tauri/src/extensions/firmware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ pub(super) fn identify(payload: &[u8], model: String) -> Option<DeviceInfo> {
eject_reboot: locate(payload, super::eject::FIRMWARE_PATH).is_some(),
repriseos: version.into(),
revision: revision.map(str::to_owned),
api: capabilities.map(|(api, _)| format!("{}.{}", api >> 16, api & 0xffff)),
api: capabilities.map(|(api, _)| super::api_text(api)),
firmware_id: capabilities.map(|(_, id)| format!("{id:08x}")),
sdk_floor: capabilities
.and_then(|(api, _)| super::sdk_floor(api))
.map(super::api_text),
})
}
36 changes: 32 additions & 4 deletions src-tauri/src/extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ fn word(data: &[u8], i: usize) -> Option<u32> {
Some(u32::from_le_bytes(data.get(i..i + 4)?.try_into().ok()?))
}

/// The catalog's "major.minor" form of an RVX API word.
fn api_text(api: u32) -> String {
format!("{}.{}", api >> 16, api & 0xffff)
}

/// The APIs at which the core's package layout changed: rv_row and
/// rv_menu_entry grew in 1.86 and rv_pane in 1.89, and rv_pane_extra changed in
/// 1.92. A core loads packages an SDK at or above the last entry at or below
/// its own API packed.
const LAYOUT_APIS: [u32; 3] = [0x10056, 0x10059, 0x1005c];

/// The lowest header SDK a core at `api` loads, when it refuses older ones.
fn sdk_floor(api: u32) -> Option<u32> {
LAYOUT_APIS
.into_iter()
.rev()
.find(|&floor| api >= floor && api >> 16 == floor >> 16)
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DeviceInfo {
Expand All @@ -24,6 +43,8 @@ pub struct DeviceInfo {
revision: Option<String>,
api: Option<String>,
firmware_id: Option<String>,
/// The lowest SDK the firmware loads packages from, when it refuses older ones.
sdk_floor: Option<String>,
}

impl DeviceInfo {
Expand All @@ -40,10 +61,12 @@ impl DeviceInfo {
let Some((major, minor)) = api.split_once('.') else {
return false;
};
major.parse::<u32>() == Ok(pkg.minimum >> 16)
&& minor
.parse::<u32>()
.is_ok_and(|v| v >= (pkg.minimum & 0xffff))
let (Ok(major), Ok(minor)) = (major.parse::<u32>(), minor.parse::<u32>()) else {
return false;
};
major == pkg.minimum >> 16
&& minor >= (pkg.minimum & 0xffff)
&& sdk_floor(major << 16 | minor).is_none_or(|floor| pkg.sdk >= floor)
&& u32::from_str_radix(target, 16) == Ok(pkg.target)
}
_ => dev,
Expand All @@ -61,6 +84,11 @@ pub struct Installed {
#[serde(skip)]
path: PathBuf,
library: bool,
/// The package's minimum API and target firmware, in catalog form.
api: String,
firmware_id: String,
/// The SDK that packed it, in catalog form.
sdk: String,
}

#[derive(Serialize)]
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/src/extensions/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub(super) struct Package {
pub minimum: u32,
pub library: bool,
pub target: u32,
/// The header's SDK API, at or above `minimum`.
pub sdk: u32,
}

pub(super) fn valid_id(id: &str) -> bool {
Expand Down Expand Up @@ -85,5 +87,6 @@ pub(super) fn parse(bytes: &[u8]) -> Result<Package> {
minimum,
library: short(46) == 5,
target: w(12)?,
sdk: w(8)?,
})
}
3 changes: 3 additions & 0 deletions src-tauri/src/extensions/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ pub(super) fn inventory(root: &Path) -> Result<Vec<Installed>> {
version: pkg.version,
requires: pkg.requires,
library: pkg.library,
api: super::api_text(pkg.minimum),
firmware_id: format!("{:08x}", pkg.target),
sdk: super::api_text(pkg.sdk),
folder: which.into(),
path,
});
Expand Down
85 changes: 76 additions & 9 deletions src-tauri/src/extensions/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ use sha2::{Digest, Sha256};
use std::fs;

fn image(version: &str) -> Vec<u8> {
core(version, 0x10051)
}

fn core(version: &str, api: u32) -> Vec<u8> {
let mut bytes =
format!("\0Build: abc12345-dirty\0RepriseOS {version}\0RepriseRVX1\0").into_bytes();
bytes.extend(0x10051u32.to_le_bytes());
bytes.extend(api.to_le_bytes());
bytes.extend(0x660eb6ffu32.to_le_bytes());
bytes
}

fn request(id: &str, version: &str, minor: u32, dependency: Option<&str>) -> WritePackage {
packed(id, version, minor, 0x10060, dependency)
}

/// A package whose manifest asks for API 1.`minor`, packed by SDK `sdk`.
fn packed(id: &str, version: &str, minor: u32, sdk: u32, dependency: Option<&str>) -> WritePackage {
let mut manifest = vec![0; 64];
let mut strings = Vec::new();
for (index, value) in [id, id, "extension", version, "author", "MIT", ""]
Expand Down Expand Up @@ -47,7 +56,7 @@ fn request(id: &str, version: &str, minor: u32, dependency: Option<&str>) -> Wri
let hash = manifest.iter().fold(2166136261u32, |h, b| {
(h ^ u32::from(*b)).wrapping_mul(16777619)
});
let mut data: Vec<_> = [0x30585652u32, 1, 0x10060, 0x660eb6ff, len, 1, hash]
let mut data: Vec<_> = [0x30585652u32, 1, sdk, 0x660eb6ff, len, 1, hash]
.into_iter()
.flat_map(u32::to_le_bytes)
.collect();
Expand Down Expand Up @@ -81,16 +90,30 @@ fn mounted_install_update_and_remove() {
let dir = folder(root, PRIMARY, false).unwrap().unwrap();
fs::rename(dir.join("crossfeed.rvx"), dir.join("custom-name.rvx")).unwrap();
write(&mount, request("crossfeed", "1.1.0", 81, Some("dsp"))).unwrap();
let installed = inventory(root).unwrap();
let crossfeed = installed.iter().find(|i| i.id == "crossfeed").unwrap();
assert_eq!(crossfeed.version, "1.1.0");
assert_eq!(
inventory(root)
.unwrap()
.iter()
.find(|i| i.id == "crossfeed")
.unwrap()
.version,
"1.1.0"
(
crossfeed.api.as_str(),
crossfeed.firmware_id.as_str(),
crossfeed.sdk.as_str()
),
("1.81", "660eb6ff", "1.96")
);
assert!(write(&mount, request("crossfeed", "1.2.0", 82, Some("dsp"))).is_err());
// A copy placed without the installer still reports the API it needs.
fs::write(
dir.join("newer.rvx"),
request("newer", "1.0.0", 84, None).data,
)
.unwrap();
let installed = inventory(root).unwrap();
assert_eq!(
installed.iter().find(|i| i.id == "newer").unwrap().api,
"1.84"
);
fs::remove_file(dir.join("newer.rvx")).unwrap();
remove(&mount, "crossfeed", PRIMARY).unwrap();
remove(&mount, "dsp", PRIMARY).unwrap();
assert!(inventory(root).unwrap().is_empty());
Expand All @@ -116,6 +139,50 @@ fn unknown_development_can_install_and_old_release_cannot() {
.allows(&ext));
}

#[test]
fn layout_floor_refuses_packages_from_older_sdks() {
let old = parse(&packed("sample", "1.0.0", 82, 0x10058, None).data).unwrap();
let rebuilt = parse(&packed("sample", "1.0.1", 82, 0x10059, None).data).unwrap();
let before = firmware::identify(&core("0.4.0", 0x10052), "iPod".into()).unwrap();
assert_eq!(before.sdk_floor, None);
assert!(before.allows(&old) && before.allows(&rebuilt));
let pre_rows = parse(&packed("sample", "0.9.0", 82, 0x10055, None).data).unwrap();
for api in [0x10056, 0x10058] {
let device = firmware::identify(&core("0.5.0", api), "iPod".into()).unwrap();
assert_eq!(device.sdk_floor.as_deref(), Some("1.86"));
assert!(!device.allows(&pre_rows));
assert!(device.allows(&old) && device.allows(&rebuilt));
}
for (version, api) in [
("0.5.0", 0x10059),
("0.5.0", 0x1005b),
("Development", 0x10059),
] {
let device = firmware::identify(&core(version, api), "iPod".into()).unwrap();
assert_eq!(device.sdk_floor.as_deref(), Some("1.89"));
assert!(!device.allows(&old));
assert!(device.allows(&rebuilt));
}
let current = parse(&packed("sample", "1.0.2", 82, 0x1005c, None).data).unwrap();
for api in [0x1005c, 0x1005d] {
let device = firmware::identify(&core("0.5.0", api), "iPod".into()).unwrap();
assert_eq!(device.sdk_floor.as_deref(), Some("1.92"));
assert!(!device.allows(&old) && !device.allows(&rebuilt));
assert!(device.allows(&current));
}
let next_major = firmware::identify(&core("0.5.0", 0x20000), "iPod".into()).unwrap();
assert_eq!(next_major.sdk_floor, None);
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
fs::create_dir_all(root.join("iPod_Control/Device")).unwrap();
fs::write(root.join("osos-cfw.bin"), core("0.5.0", 0x10059)).unwrap();
fs::write(root.join("cfw-loader.bin"), vec![0; 0x25220]).unwrap();
let mount = inspect(root).unwrap().unwrap();
assert!(write(&mount, packed("sample", "1.0.0", 82, 0x10058, None)).is_err());
write(&mount, packed("sample", "1.0.1", 82, 0x10059, None)).unwrap();
assert_eq!(inventory(root).unwrap()[0].sdk, "1.89");
}

#[test]
#[ignore = "requires a mounted RepriseOS iPod; reads only"]
fn read_connected_ipod() {
Expand Down
24 changes: 14 additions & 10 deletions src/components/extensions/ExtensionDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ const ExtensionDetails: React.FC<IExtensionDetailsProps> = ({ state, entry, onOp
const busy = operationRunning(state);
const deviceReady = state.device.status === "ready";
const compatible = !status.compatibility || status.compatibility.compatible;
const copy = status.installedCompatibility;
let note = formatSize(entry.package.size);
if (copy && !copy.compatible) {
note = copy.reason;
} else if (status.updateAvailable && status.compatibility && !status.compatibility.compatible) {
const { reason } = status.compatibility;
note = `Version ${entry.version} ${reason[0].toLowerCase()}${reason.slice(1)}`;
} else if (status.installed) {
note = `Version ${status.installed} installed`;
} else if (status.compatibility && !status.compatibility.compatible) {
note = status.compatibility.reason;
}
const requires = entry.requires.map(
(id) => state.catalog.entries.find((item) => item.id === id) ?? { id, name: id },
);
Expand Down Expand Up @@ -83,7 +95,7 @@ const ExtensionDetails: React.FC<IExtensionDetailsProps> = ({ state, entry, onOp
<Button.Label>Remove</Button.Label>
</Button.Root>
)}
{(!status.installed || status.updateAvailable) && (
{(!status.installed || status.updateAvailable || status.canReplace) && (
<Button.Root
disabled={busy || !deviceReady || !compatible}
onClick={() => void installExtension(entry.id)}
Expand All @@ -93,15 +105,7 @@ const ExtensionDetails: React.FC<IExtensionDetailsProps> = ({ state, entry, onOp
)}
</div>
)}
{indicator ?? (
<p className="text-[12px] text-body">
{status.compatibility && !status.compatibility.compatible
? status.compatibility.reason
: status.installed
? `Version ${status.installed} installed`
: formatSize(entry.package.size)}
</p>
)}
{indicator ?? <p className="text-[12px] text-body">{note}</p>}
</div>
</div>
{steps}
Expand Down
40 changes: 24 additions & 16 deletions src/components/extensions/ExtensionRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type ExtensionsState,
entryStatus,
installExtension,
installedCompatibility,
operationRunning,
removeExtension,
} from "../../lib/extensions";
Expand All @@ -32,17 +33,26 @@ const ExtensionRow: React.FC<IExtensionRowProps> = ({ state, entry, copies, mode
const deviceReady = state.device.status === "ready";
const legacy = copies?.some((copy) => copy.folder === "/Revola/Extensions/");

// An installed copy is judged by its own package, the catalog version only for installing.
const problem = status
? status.installed
? status.installedCompatibility
: status.compatibility
: installedCompatibility(id, state);
const compatible = !status?.compatibility || status.compatibility.compatible;
const update = !!status?.updateAvailable && compatible;

const version = status?.installed ?? copies?.[0]?.version ?? entry?.version;
const versionText = entry
? status?.updateAvailable
? `${status.installed} → ${entry.version}`
? update
? `${status?.installed} → ${entry.version}`
: (version ?? "")
: `${version} · Not in the catalog`;

let label: { text: string; tone: "ok" | "update" | "warn" } | null = null;
if (status?.compatibility && !status.compatibility.compatible) {
label = { text: status.compatibility.reason, tone: "warn" };
} else if (status?.updateAvailable) {
if (problem && !problem.compatible) {
label = { text: problem.reason, tone: "warn" };
} else if (update) {
label = { text: "Update available", tone: "update" };
} else if (status?.installed) {
label = { text: "Installed", tone: "ok" };
Expand All @@ -52,15 +62,13 @@ const ExtensionRow: React.FC<IExtensionRowProps> = ({ state, entry, copies, mode
label = { text: "Compatible", tone: "ok" };
}

const compatible = !status?.compatibility || status.compatibility.compatible;
const action =
status?.updateAvailable && compatible
? "update"
: mode === "installed"
? "remove"
: !status?.installed && compatible
? "install"
: null;
const action = update
? "update"
: mode === "installed"
? "remove"
: (!status?.installed && compatible) || status?.canReplace
? "install"
: null;

return (
<li className="border-t border-[#e2dfda] first:border-t-0">
Expand Down Expand Up @@ -97,9 +105,9 @@ const ExtensionRow: React.FC<IExtensionRowProps> = ({ state, entry, copies, mode
)}
</div>
{indicator ??
(mode === "browse" && status?.compatibility?.compatible === false ? (
(mode === "browse" && problem?.compatible === false && !action ? (
<span className="shrink-0 whitespace-nowrap text-[12px] text-body">
{status.compatibility.summary}
{problem.summary}
</span>
) : (
<div className="flex shrink-0 items-center gap-3">
Expand Down
Loading