12 const callPackagelist = rpc.declare({
14 method: 'packagelist',
17 const callSystemBoard = rpc.declare({
22 const callUpgradeStart = rpc.declare({
24 method: 'upgrade_start',
29 * Returns the branch of a given version. This helps to offer upgrades
30 * for point releases (aka within the branch).
33 * SNAPSHOT -> SNAPSHOT
34 * 21.02-SNAPSHOT -> 21.02
35 * 21.02.0-rc1 -> 21.02
38 * @param {string} version
39 * Input version from which to determine the branch
41 * The determined branch
43 function get_branch(version) {
44 return version.replace('-SNAPSHOT', '').split('.').slice(0, 2).join('.');
48 * The OpenWrt revision string contains both a hash as well as the number
49 * commits since the OpenWrt/LEDE reboot. It helps to determine if a
50 * snapshot is newer than another.
52 * @param {string} revision
53 * Revision string of a OpenWrt device
55 * The number of commits since OpenWrt/LEDE reboot
57 function get_revision_count(revision) {
58 return parseInt(revision.substring(1).split('-')[0]);
63 init: [ 0, _('Received build request')],
64 container_setup: [ 10, _('Setting up ImageBuilder')],
65 validate_revision: [ 20, _('Validating revision')],
66 validate_manifest: [ 30, _('Validating package selection')],
67 calculate_packages_hash: [ 40, _('Calculating package hash')],
68 building_image: [ 50, _('Generating firmware image')],
69 signing_images: [ 95, _('Signing images')],
70 done: [100, _('Completed generating firmware image')],
71 failed: [100, _('Failed to generate firmware image')],
73 /* Obsolete status values, retained for backward compatibility. */
74 download_imagebuilder: [ 20, _('Downloading ImageBuilder archive')],
75 unpack_imagebuilder: [ 40, _('Setting Up ImageBuilder')],
81 selectImage: function (images, data, firmware) {
82 var filesystemFilter = function(e) {
83 return (e.filesystem == firmware.filesystem);
85 var typeFilter = function(e) {
86 let efi_targets = ['armsr', 'loongarch', 'x86'];
87 let efi_capable = efi_targets.some((tgt) => firmware.target.startsWith(tgt));
90 return (e.type == 'combined-efi');
92 return (e.type == 'combined');
95 return (e.type == 'sysupgrade' || e.type == 'combined');
98 return images.filter(filesystemFilter).filter(typeFilter)[0];
101 handle200: function (response, content, data, firmware) {
102 response = response.json();
103 let image = this.selectImage(response.images, data, firmware);
105 if (image.name != undefined) {
106 this.sha256_unsigned = image.sha256_unsigned;
107 let sysupgrade_url = `${data.url}/store/${response.bin_dir}/${image.name}`;
109 let keep = E('input', { type: 'checkbox' });
114 `${response.version_number} ${response.version_code}`,
119 if (data.advanced_mode == 1) {
136 E('a', { href: sysupgrade_url }, _('Download firmware image'))
138 if (data.rebuilder) {
139 fields.push(_('Rebuilds'), E('div', { id: 'rebuilder_status' }));
142 let table = E('div', { class: 'table' });
144 for (let i = 0; i < fields.length; i += 2) {
146 E('tr', { class: 'tr' }, [
147 E('td', { class: 'td left', width: '33%' }, [fields[i]]),
148 E('td', { class: 'td left' }, [fields[i + 1]]),
158 E('label', { class: 'btn' }, [
161 _('Keep settings and retain the current configuration'),
164 E('div', { class: 'right' }, [
165 E('div', { class: 'btn', click: ui.hideModal }, _('Cancel')),
170 class: 'btn cbi-button cbi-button-positive important',
171 click: ui.createHandlerFn(this, function () {
172 this.handleInstall(sysupgrade_url, keep.checked, image.sha256);
175 _('Install firmware image')
180 ui.showModal(_('Successfully created firmware image'), modal_body);
181 if (data.rebuilder) {
182 this.handleRebuilder(content, data, firmware);
187 handle202: function (response) {
188 response = response.json();
189 this.request_hash = response.request_hash;
191 if ('queue_position' in response) {
192 ui.showModal(_('Queued...'), [
195 { class: 'spinning' },
196 _('Request in build queue position %s').format(
197 response.queue_position
202 ui.showModal(_('Building Firmware...'), [
205 { class: 'spinning' },
206 _('Progress: %s%% %s').format(
207 this.steps[response.imagebuilder_status][0],
208 this.steps[response.imagebuilder_status][1]
215 handleError: function (response, data, firmware) {
216 response = response.json();
217 const request_data = {
219 request_hash: this.request_hash,
220 sha256_unsigned: this.sha256_unsigned,
224 E('p', {}, _('Server response: %s').format(response.detail)),
227 { href: 'https://github.com/openwrt/asu/issues' },
228 _('Please report the error message and request')
230 E('p', {}, _('Request Data:')),
231 E('pre', {}, JSON.stringify({ ...request_data }, null, 4)),
234 if (response.stdout) {
235 body.push(E('b', {}, 'STDOUT:'));
236 body.push(E('pre', {}, response.stdout));
239 if (response.stderr) {
240 body.push(E('b', {}, 'STDERR:'));
241 body.push(E('pre', {}, response.stderr));
245 E('div', { class: 'right' }, [
246 E('div', { class: 'btn', click: ui.hideModal }, _('Close')),
250 ui.showModal(_('Error building the firmware image'), body);
253 handleRequest: function (server, main, content, data, firmware) {
254 let request_url = `${server}/api/v1/build`;
256 let local_content = content;
259 * If `request_hash` is available use a GET request instead of
260 * sending the entire object.
262 if (this.request_hash && main == true) {
263 request_url += `/${this.request_hash}`;
269 .request(request_url, { method: method, content: local_content })
270 .then((response) => {
271 switch (response.status) {
274 this.handle202(response);
276 response = response.json();
278 let view = document.getElementById(server);
279 view.innerText = `⏳ (${
280 this.steps[response.imagebuilder_status][0]
286 poll.remove(this.pollFn);
287 this.handle200(response, content, data, firmware);
289 poll.remove(this.rebuilder_polls[server]);
290 response = response.json();
291 let view = document.getElementById(server);
292 let image = this.selectImage(response.images, data, firmware);
293 if (image.sha256_unsigned == this.sha256_unsigned) {
294 view.innerText = '✅ %s'.format(server);
296 view.innerHTML = `⚠️ ${server} (<a href="${server}/store/${
298 }/${image.name}">${_('Download')}</a>)`;
302 case 400: // bad request
303 case 422: // bad package
304 case 500: // build failed
306 poll.remove(this.pollFn);
307 this.handleError(response, data, firmware);
310 poll.remove(this.rebuilder_polls[server]);
311 document.getElementById(server).innerText = '🚫 %s'.format(
319 handleRebuilder: function (content, data, firmware) {
320 this.rebuilder_polls = {};
321 for (let rebuilder of data.rebuilder) {
322 this.rebuilder_polls[rebuilder] = L.bind(
331 poll.add(this.rebuilder_polls[rebuilder], 5);
332 document.getElementById(
334 ).innerHTML += `<p id="${rebuilder}">⏳ ${rebuilder}</p>`;
339 handleInstall: function (url, keep, sha256) {
340 ui.showModal(_('Downloading...'), [
343 { class: 'spinning' },
344 _('Downloading firmware from server to browser')
351 'Content-Type': 'application/x-www-form-urlencoded',
353 responseType: 'blob',
355 .then((response) => {
356 let form_data = new FormData();
357 form_data.append('sessionid', rpc.getSessionID());
358 form_data.append('filename', '/tmp/firmware.bin');
359 form_data.append('filemode', 600);
360 form_data.append('filedata', response.blob());
362 ui.showModal(_('Uploading...'), [
365 { class: 'spinning' },
366 _('Uploading firmware from browser to device')
371 .get(`${L.env.cgi_base}/cgi-upload`, {
375 .then((response) => response.json())
376 .then((response) => {
377 if (response.sha256sum != sha256) {
378 ui.showModal(_('Wrong checksum'), [
381 _('Error during download of firmware. Please try again')
383 E('div', { class: 'btn', click: ui.hideModal }, _('Close')),
386 ui.showModal(_('Installing...'), [
389 { class: 'spinning' },
390 _('Installing the sysupgrade. Do not unpower device!')
394 L.resolveDefault(callUpgradeStart(keep), {}).then((response) => {
396 ui.awaitReconnect(window.location.host);
398 ui.awaitReconnect('192.168.1.1', 'openwrt.lan');
406 handleCheck: function (data, firmware) {
407 this.request_hash = '';
408 let { url, revision, advanced_mode, branch } = data;
409 let { version, target, profile, packages } = firmware;
412 const endpoint = version.endsWith('SNAPSHOT') ? `revision/${version}/${target}` : 'overview';
413 const request_url = `${url}/api/v1/${endpoint}`;
415 ui.showModal(_('Searching...'), [
418 { class: 'spinning' },
419 _('Searching for an available sysupgrade of %s - %s').format(
426 L.resolveDefault(request.get(request_url)).then((response) => {
428 ui.showModal(_('Error connecting to upgrade server'), [
432 _('Could not reach API at "%s". Please try again later.').format(
436 E('pre', {}, response.responseText),
437 E('div', { class: 'right' }, [
438 E('div', { class: 'btn', click: ui.hideModal }, _('Close')),
443 if (version.endsWith('SNAPSHOT')) {
444 const remote_revision = response.json().revision;
446 get_revision_count(revision) < get_revision_count(remote_revision)
448 candidates.push([version, remote_revision]);
451 const latest = response.json().latest;
453 for (let remote_version of latest) {
454 let remote_branch = get_branch(remote_version);
456 // already latest version installed
457 if (version == remote_version) {
461 // skip branch upgrades outside the advanced mode
462 if (branch != remote_branch && advanced_mode == 0) {
466 candidates.unshift([remote_version, null]);
468 // don't offer branches older than the current
469 if (branch == remote_branch) {
475 // allow to re-install running firmware in advanced mode
476 if (advanced_mode == 1) {
477 candidates.unshift([version, revision]);
480 if (candidates.length) {
486 version: candidates[0][0],
487 packages: Object.keys(packages).sort(),
491 let map = new form.JSONMap(mapdata, '');
498 'Use defaults for the safest update'
500 o = s.option(form.ListValue, 'version', 'Select firmware version');
501 for (let candidate of candidates) {
502 if (candidate[0] == version && candidate[1] == revision) {
505 _('[installed] %s').format(
507 ? `${candidate[0]} - ${candidate[1]}`
514 candidate[1] ? `${candidate[0]} - ${candidate[1]}` : candidate[0]
519 if (advanced_mode == 1) {
520 o = s.option(form.Value, 'profile', _('Board Name / Profile'));
521 o = s.option(form.DynamicList, 'packages', _('Packages'));
524 L.resolveDefault(map.render()).then((form_rendered) => {
525 ui.showModal(_('New firmware upgrade available'), [
528 _('Currently running: %s - %s').format(
534 E('div', { class: 'right' }, [
535 E('div', { class: 'btn', click: ui.hideModal }, _('Cancel')),
540 class: 'btn cbi-button cbi-button-positive important',
541 click: ui.createHandlerFn(this, function () {
542 map.save().then(() => {
545 packages: mapdata.request.packages,
546 version: mapdata.request.version,
547 profile: mapdata.request.profile
549 this.pollFn = L.bind(function () {
550 this.handleRequest(url, true, content, data, firmware);
552 poll.add(this.pollFn, 5);
557 _('Request firmware image')
563 ui.showModal(_('No upgrade available'), [
566 _('The device runs the latest firmware version %s - %s').format(
571 E('div', { class: 'right' }, [
572 E('div', { class: 'btn', click: ui.hideModal }, _('Close')),
579 load: async function () {
580 const promises = await Promise.all([
581 L.resolveDefault(callPackagelist(), {}),
582 L.resolveDefault(callSystemBoard(), {}),
583 L.resolveDefault(fs.stat('/sys/firmware/efi'), null),
584 uci.load('attendedsysupgrade'),
587 url: uci.get_first('attendedsysupgrade', 'server', 'url'),
588 branch: get_branch(promises[1].release.version),
589 revision: promises[1].release.revision,
591 advanced_mode: uci.get_first('attendedsysupgrade', 'client', 'advanced_mode') || 0,
592 rebuilder: uci.get_first('attendedsysupgrade', 'server', 'rebuilder')
595 client: 'luci/' + promises[0].packages['luci-app-attendedsysupgrade'],
596 packages: promises[0].packages,
597 profile: promises[1].board_name,
598 target: promises[1].release.target,
599 version: promises[1].release.version,
601 filesystem: promises[1].rootfs_type
603 return [data, firmware];
606 render: function (response) {
607 const data = response[0];
608 const firmware = response[1];
611 E('h2', _('Attended Sysupgrade')),
615 'The attended sysupgrade service allows to upgrade vanilla and custom firmware images easily.'
621 'This is done by building a new firmware on demand via an online service.'
626 _('Currently running: %s - %s').format(
634 class: 'btn cbi-button cbi-button-positive important',
635 click: ui.createHandlerFn(this, this.handleCheck, data, firmware),
637 _('Search for firmware upgrade')
641 handleSaveApply: null,