summaryrefslogtreecommitdiffstats
path: root/assets/js/rootLantiq.js
blob: f13690ce82736762b6752c9a50abc5c3a13793c8 (plain) (blame)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const LOAD_ADDR = "80800000"
const IMAGE0_ADDR = "C0000 740000";
const IMAGE1_ADDR = "800000 800000";

async function detectUboot(serial) {
    await serial.readLine((line) => {
        if (line.startsWith('U-Boot')) {
            return true;
        }
    });
}

async function waitUbootStop(serial, sfpModel, outputMsgCallback) {
    const interruptChar = String.fromCharCode(3);

    const interval = setInterval(function() {
        serial.writeString(interruptChar);
    }, 10);

    await detectUboot(serial);
    outputMsgCallback(`Root in progress: Trigger characters received. DO NOT TOUCH THE ${sfpModel} UNTIL THE PROCEDURE IS COMPLETED!`);
    await delay(5000);
    clearInterval(interval);
}

async function checkUbootUnlocked(serial) {
    let unlocked = false;

    await serial.readLine((line) => {
        if (line.startsWith('Press SPACE to delay and Ctrl-C to abort autoboot')) {
            unlocked = true;
            return true
        }
    }, 2000);

    return unlocked;
}

async function waitFailbackShell(serial, outputMsgCallback) {
    await serial.readLine((line) => {
        if (line.startsWith('Press the [f] key and hit [enter] to enter failsafe mode')) {
            return true;
        }
    });

    let interval = setInterval(function() {
        serial.writeString('f\n');
    }, 10);

    outputMsgCallback("Root in progress: Trigger characters received. Waiting for boot to end...");
    await delay(3000);
    clearInterval(interval);

    interval = setInterval(function() {
        serial.writeString(String.fromCharCode(10));
    }, 10);

    await serial.readLine((line) => {
        if (line.includes('root@(none)')) {
            return true;
        }
    });

    await delay(1000);
    clearInterval(interval);
}

async function lantiqRootUboot(port, sfpModel, outputMsgCallback, outputErrorCallback, baudRate = 115200) {
    const serial = new SerialReadWrite(port, baudRate);

    try {
        outputMsgCallback(`Please disconnect the ${sfpModel} from the SFP adapter if it is currently plugged in!`);
        await delay(10000);
        outputMsgCallback(`Now you need to insert the ${sfpModel} into the SFP adapter, if the procedure does not go ahead, check the connections and then remove and reconnect the ${sfpModel} again`);

        while(true) {
            await waitUbootStop(serial, sfpModel, outputMsgCallback);
            const ubootUnlocked = await checkUbootUnlocked(serial);

            if (ubootUnlocked == true) {
                break;
            }

            outputMsgCallback("Root in progress: Set U-Boot bootdelay to 5...");
            await serial.writeString('setenv bootdelay 5\n');
            await delay(1000);
            outputMsgCallback("Root in progress: Enable ASC serial...");
            await serial.writeString('setenv asc0 0\n');
            await delay(1000);
            outputMsgCallback("Root in progress: Set GPIO to unlock serial...");
            await serial.writeString('setenv preboot "gpio set 3;gpio input 2;gpio input 105;gpio input 106;gpio input 107;gpio input 108"\n');
            await delay(1000);
            outputMsgCallback("Root in progress: Save changes...");
            await serial.writeString('saveenv\n');
            await delay(1000);
            outputMsgCallback("Root in progress: Rebooting...");
            await serial.writeString('reset\n');
            await delay(1000);
        }

        return true;
    } catch (err) {
        outputErrorCallback(`Error: ${err.message}`);
        return false;
    } finally {
        await serial.closePort();
    }
}

async function unlockHuaweiShell(port, outputMsgCallback, outputErrorCallback, baudRate = 115200) {
    const serial = new SerialReadWrite(port, baudRate);

    try {
        outputMsgCallback("Root in progress: Rebooting...");
        await serial.writeString('reset\n');
        await delay(1000);
        outputMsgCallback("Waiting for reboot");
        await waitFailbackShell(serial, outputMsgCallback);
        outputMsgCallback("Root in progress: Enable full Linux shell...");
        await serial.writeString('mount_root && mkdir -p /overlay/etc && sed "s|/opt/lantiq/bin/minishell|/bin/ash|g" /rom/etc/passwd > /overlay/etc/passwd\n');
        await delay(1000);
        outputMsgCallback("Root in progress: Umount rootfs partitions...");
        await serial.writeString('umount /overlay && umount -a\n');
        await delay(1000);
        return true;
    } catch (err) {
        outputErrorCallback(`Error: ${err.message}`);
        return false;
    } finally {
        await serial.closePort();
    }
}

async function changeBaudrate(port, newBaudrate, currBaudrate, outputErrorCallback) {
    let reader,writer, readableStreamClosed, writerStreamClosed;

    try {
        ({ reader, writer, readableStreamClosed, writerStreamClosed } = await openPortLineBreak(port, currBaudrate));
        await writer.write(`setenv baudrate ${newBaudrate}\n`);
        await delay(1000);
        await closePortLineBreak(port, reader, writer, readableStreamClosed, writerStreamClosed);
        ({ reader, writer, readableStreamClosed, writerStreamClosed } = await openPortLineBreak(port, newBaudrate));

        const interval = setInterval(function() {
            writer.write(String.fromCharCode(13));
        }, 10);

        while (true) {
            const { value, done } = await reader.read();

            if (value.startsWith('FALCON')) {
                clearInterval(interval);
                break;
            }
        }

        await closePortLineBreak(port, reader, writer, readableStreamClosed, writerStreamClosed);
        return true;
    } catch (err) {
        outputErrorCallback(`Error: ${err.message}`);
        await closePortLineBreak(port, reader, writer, readableStreamClosed, writerStreamClosed);
        return false;
    }
}

async function sendImageMtd(port, data, baudRate, outputErrorCallback, progressCallback) {
    let reader,writer, readableStreamClosed, writerStreamClosed;

    try {
        ({ reader, writer, readableStreamClosed, writerStreamClosed } = await openPortLineBreak(port, baudRate));
        await writer.write(`loady 0x${LOAD_ADDR}\n`);
        await delay(1000);
        await closePortLineBreak(port, reader, writer, readableStreamClosed, writerStreamClosed);   /* XYMini needs reopen the port */
    } catch (err) {
        outputErrorCallback(`Error: ${err.message}`);
        await closePortLineBreak(port, reader, writer, readableStreamClosed, writerStreamClosed);
        return false;
    }

    try {
        await port.open({ baudRate: baudRate });
        reader = port.readable.getReader();
        writer = port.writable.getWriter();

        await sendXYMini(reader, writer, data, baudRate, progressCallback);
        await reader.cancel();
        await writer.close();
        await port.close();
        return true;
    } catch (err) {
        await reader.cancel();
        await writer.close();
        await port.close();
        outputErrorCallback(`Error: ${err.message}`);
        return false;
    }
}

async function waitEndImageLoad(port, baudRate, outputErrorCallback) {
    let reader, writer, readableStreamClosed, writerStreamClosed;

    try {
        ({ reader, writer, readableStreamClosed, writerStreamClosed } = await openPortLineBreak(port, baudRate));

        while (true) {
            const { value, done } = await reader.read();

            if (value.includes('Total Size')) {
                break;
            }
        }

        await delay(1000);
        await closePortLineBreak(port, reader, writer, readableStreamClosed, writerStreamClosed);
        return true;
    } catch (err) {
        outputErrorCallback(`Error: ${err.message}`);
        await closePortLineBreak(port, reader, writer, readableStreamClosed, writerStreamClosed);
        return false;
    }
}

async function flashImageMtd(port, image, baudRate, outputErrorCallback) {
    let reader, writer, readableStreamClosed, writerStreamClosed;

    try {
        ({ reader, writer, readableStreamClosed, writerStreamClosed } = await openPortLineBreak(port, baudRate));
        if (image == "image0") {
            await writer.write(`sf probe 0 && sf erase ${IMAGE0_ADDR} && sf write ${LOAD_ADDR} ${IMAGE0_ADDR} && setenv committed_image 0 && setenv image0_is_valid 1 && saveenv && reset\n`);
        } else {
            await writer.write(`sf probe 0 && sf erase ${IMAGE1_ADDR} && sf write ${LOAD_ADDR} ${IMAGE1_ADDR} && setenv committed_image 1 && setenv image1_is_valid 1 && saveenv && reset\n`);
        }
    } catch (err) {
        outputErrorCallback(`Error: ${err.message}`);
        return false;
    } finally {
        await closePortLineBreak(port, reader, writer, readableStreamClosed, writerStreamClosed);
    }

    const serial = new SerialReadWrite(port, baudRate);
    try {
        /* Wait to avoid the user from disconnecting the SFP while the image is being flashed */
        await delay(1000);
        await detectUboot(serial);
        return true;
    } catch (err) {
        outputErrorCallback(`Error: ${err.message}`);
        return false;
    } finally {
        await serial.closePort();
    }
}