summaryrefslogtreecommitdiff
path: root/static/js/web-install.js
blob: 85cd195b58fa14dce66ac97339a3afa346ed7295 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT

async function adbRebootBootloader() {
    const webusb = await Adb.open("WebUSB");

    if (!webusb.isAdb()) {
        console.log("error: not in adb mode");
        return;
    }

    console.log("connecting with adb");

    const adb = await webusb.connectAdb("host::");
    await adb.reboot("bootloader");
}

async function unlockBootloader() {
    const webusb = await Adb.open("WebUSB");

    if (!webusb.isFastboot()) {
        console.log("error: not in fastboot mode");
    }

    console.log("connecting with fastboot");

    const fastboot = await webusb.connectFastboot();
    await fastboot.send("flashing unlock");
    await fastboot.receive();
}

async function downloadRelease() {
    const webusb = await Adb.open("WebUSB");

    if (!webusb.isFastboot()) {
        console.log("error: not in fastboot mode");
    }

    console.log("connecting with fastboot");

    const fastboot = await webusb.connectFastboot();
    await fastboot.send("getvar:product");
    const response = await fastboot.receive();
    if (fastboot.get_cmd(response) == "FAIL") {
        throw new Error("getvar product failed");
    }
    const decoder = new TextDecoder();
    const product = decoder.decode(fastboot.get_payload(response));

    const baseUrl = "https://releases.grapheneos.org/";
    const metadata = await (await fetch(baseUrl + product + "-stable")).text();
    const buildNumber = metadata.split(" ")[0];
    const downloadUrl = baseUrl + product + "-factory-" + buildNumber + ".zip";
    console.log(downloadUrl);

    // Download factory images
    //
    // Need to do this in a way that works well with huge files, particularly since the zip needs
    // to be extracted. Could potentially split it up on the server if this works out badly.
}

async function lockBootloader() {
    const webusb = await Adb.open("WebUSB");

    if (!webusb.isFastboot()) {
        console.log("error: not in fastboot mode");
    }

    console.log("connecting with fastboot");

    const fastboot = await webusb.connectFastboot();
    await fastboot.send("flashing lock");
    await fastboot.receive();
}

if ("usb" in navigator) {
    console.log("WebUSB available");

    const adbRebootBootloaderButton = document.getElementById("adb-reboot-bootloader");
    adbRebootBootloaderButton.disabled = false;
    adbRebootBootloaderButton.onclick = adbRebootBootloader;

    const unlockBootloaderButton = document.getElementById("unlock-bootloader");
    unlockBootloaderButton.disabled = false;
    unlockBootloaderButton.onclick = unlockBootloader;

    const downloadReleaseButton = document.getElementById("download-release");
    downloadReleaseButton.disabled = false;
    downloadReleaseButton.onclick = downloadRelease;

    const lockBootloaderButton = document.getElementById("lock-bootloader");
    lockBootloaderButton.disabled = false;
    lockBootloaderButton.onclick = lockBootloader;
} else {
    console.log("WebUSB unavailable");
}

// @license-end