From 2a5c5f47f95978c6eb5c52b41b4e931e63afa4f5 Mon Sep 17 00:00:00 2001
From: Jo-Philipp Wich
- <%_ The flash image was uploaded.
- Below is the checksum and file size listed,
- compare them with the original file to ensure data integrity.
Click "Proceed" below to start the flash procedure.')));
+ body.push(E('ul', {}, [
+ res[0].size ? E('li', {}, '%s: %1024.2mB'.format(_('Size'), res[0].size)) : '',
+ res[0].checksum ? E('li', {}, '%s: %s'.format(_('MD5'), res[0].checksum)) : '',
+ res[0].sha256sum ? E('li', {}, '%s: %s'.format(_('SHA256'), res[0].sha256sum)) : '',
+ E('li', {}, keep.checked ? _('Configuration files will be kept') : _('Caution: Configuration files will be erased'))
+ ]));
+
+ if (is_invalid || is_too_big)
+ body.push(E('hr'));
+
+ if (is_too_big)
+ body.push(E('p', { 'class': 'alert-message' }, [
+ _('It appears that you are trying to flash an image that does not fit into the flash memory, please verify the image file!')
+ ]));
+
+ if (is_invalid)
+ body.push(E('p', { 'class': 'alert-message' }, [
+ res[1].stderr ? res[1].stderr : '',
+ res[1].stderr ? E('br') : '',
+ res[1].stderr ? E('br') : '',
+ _('The uploaded image file does not contain a supported format. Make sure that you choose the generic image format for your platform.')
+ ]));
+
+ if (is_invalid || is_too_big)
+ body.push(E('p', {}, E('label', { 'class': 'btn alert-message danger' }, [
+ force, ' ', _('Force upgrade'),
+ E('br'), E('br'),
+ _('Select \'Force upgrade\' to flash the image even if the image format check fails. Use only if you are sure that the firmware is correct and meant for your device!')
+ ])));
+
+ var cntbtn = E('button', {
+ 'class': 'btn cbi-button-action important',
+ 'click': L.ui.createHandlerFn(this, 'handleSysupgradeConfirm', btn, keep.checked, force.checked),
+ 'disabled': (is_invalid || is_too_big) ? true : null
+ }, [ _('Continue') ]);
+
+ body.push(E('div', { 'class': 'right' }, [
+ E('button', {
+ 'class': 'btn',
+ 'click': L.ui.createHandlerFn(this, function(ev) {
+ return callFileRemove('/tmp/firmware.bin').finally(L.ui.hideModal);
+ })
+ }, [ _('Cancel') ]), ' ', cntbtn
+ ]));
+
+ force.addEventListener('change', function(ev) {
+ cntbtn.disabled = !ev.target.checked;
+ });
+
+ L.ui.showModal(_('Flash image?'), body);
+ }, this, ev.target))
+ .finally(L.bind(function(btn) {
+ btn.firstChild.data = _('Flash image...');
+ }, this, ev.target));
+ },
+
+ handleSysupgradeConfirm: function(btn, keep, force, ev) {
+ btn.firstChild.data = _('Flashing…');
+
+ L.ui.showModal(_('Flashing…'), [
+ E('p', { 'class': 'spinning' }, _('The system is flashing now.
DO NOT POWER OFF THE DEVICE!
Wait a few minutes before you try to reconnect. It might be necessary to renew the address of your computer to reach the device again, depending on your settings.'))
+ ]);
+
+ var opts = [];
+
+ if (!keep)
+ opts.push('-n');
+
+ if (force)
+ opts.push('--force');
+
+ opts.push('/tmp/firmware.bin');
+
+ /* Currently the sysupgrade rpc call will not return, hence no promise handling */
+ callFileExec('/sbin/sysupgrade', opts);
+
+ awaitReconnect(window.location.host, '192.168.1.1', 'openwrt.lan');
+ },
+
+ handleBackupList: function(ev) {
+ return callFileExec('/sbin/sysupgrade', [ '--list-backup' ]).then(function(res) {
+ if (res.code != 0) {
+ L.ui.addNotification(null, [
+ E('p', _('The sysupgrade command failed with code %d').format(res.code)),
+ res.stderr ? E('pre', {}, [ res.stderr ]) : ''
+ ]);
+ L.raise('Error', 'Sysupgrade failed');
+ }
+
+ L.ui.showModal(_('Backup file list'), [
+ E('p', _('Below is the determined list of files to backup. It consists of changed configuration files marked by opkg, essential base files and the user defined backup patterns.')),
+ E('ul', {}, (res.stdout || '').trim().split(/\n/).map(function(ln) { return E('li', {}, ln) })),
+ E('div', { 'class': 'right' }, [
+ E('button', {
+ 'class': 'btn',
+ 'click': L.ui.hideModal
+ }, [ _('Dismiss') ])
+ ])
+ ], 'cbi-modal');
+ });
+ },
+
+ handleBackupSave: function(m, ev) {
+ return m.save(function() {
+ return callFileWrite('/etc/sysupgrade.conf', mapdata.config.editlist.trim().replace(/\r\n/g, '\n') + '\n');
+ }).then(function() {
+ L.ui.addNotification(null, E('p', _('Contents have been saved.')), 'info');
+ }).catch(function(e) {
+ L.ui.addNotification(null, E('p', _('Unable to save contents: %s').format(e)));
+ });
+ },
+
+ render: function(rpc_replies) {
+ var has_sysupgrade = (rpc_replies[0].type == 'file'),
+ hostname = rpc_replies[1],
+ procmtd = rpc_replies[2],
+ procpart = rpc_replies[3],
+ has_rootfs_data = rpc_replies.slice(4).filter(function(n) { return n == 'rootfs_data' })[0],
+ storage_size = findStorageSize(procmtd, procpart),
+ m, s, o, ss;
+
+ m = new form.JSONMap(mapdata, _('Flash operations'));
+ m.tabbed = true;
+
+ s = m.section(form.NamedSection, 'actions', _('Actions'));
+
+
+ o = s.option(form.SectionValue, 'actions', form.NamedSection, 'actions', 'actions', _('Backup'), _('Click "Generate archive" to download a tar archive of the current configuration files.'));
+ ss = o.subsection;
+
+ o = ss.option(form.Button, 'dl_backup', _('Download backup'));
+ o.inputstyle = 'action important';
+ o.inputtitle = _('Generate archive');
+ o.onclick = this.handleBackup;
+
+
+ o = s.option(form.SectionValue, 'actions', form.NamedSection, 'actions', 'actions', _('Restore'), _('To restore configuration files, you can upload a previously generated backup archive here. To reset the firmware to its initial state, click "Perform reset" (only possible with squashfs images).'));
+ ss = o.subsection;
+
+ if (has_rootfs_data) {
+ o = ss.option(form.Button, 'reset', _('Reset to defaults'));
+ o.inputstyle = 'negative important';
+ o.inputtitle = _('Perform reset');
+ o.onclick = this.handleReset;
+ }
+
+ o = ss.option(form.Button, 'restore', _('Restore backup'), _('Custom files (certificates, scripts) may remain on the system. To prevent this, perform a factory-reset first.'));
+ o.inputstyle = 'action important';
+ o.inputtitle = _('Upload archive...');
+ o.onclick = L.bind(this.handleRestore, this);
+
+
+ o = s.option(form.SectionValue, 'actions', form.NamedSection, 'actions', 'actions', _('Save mtdblock contents'), _('Click "Save mtdblock" to download specified mtdblock file. (NOTE: THIS FEATURE IS FOR PROFESSIONALS! )'));
+ ss = o.subsection;
+
+ o = ss.option(form.ListValue, 'mtdselect', _('Choose mtdblock'));
+ procmtd.split(/\n/).forEach(function(ln) {
+ var match = ln.match(/^mtd(\d+): .+ "(.+?)"$/);
+ if (match)
+ o.value(match[1], match[2]);
+ });
+
+ o = ss.option(form.Button, 'mtddownload', _('Download mtdblock'));
+ o.inputstyle = 'action important';
+ o.inputtitle = _('Save mtdblock');
+ o.onclick = L.bind(this.handleBlock, this, hostname);
+
+
+ o = s.option(form.SectionValue, 'actions', form.NamedSection, 'actions', 'actions', _('Flash new firmware image'),
+ has_sysupgrade
+ ? _('Upload a sysupgrade-compatible image here to replace the running firmware. Check "Keep settings" to retain the current configuration (requires a compatible firmware image).')
+ : _('Sorry, there is no sysupgrade support present; a new firmware image must be flashed manually. Please refer to the wiki for device specific install instructions.'));
+
+ ss = o.subsection;
+
+ if (has_sysupgrade) {
+ o = ss.option(form.Flag, 'keep', _('Keep settings'));
+ o.default = o.enabled;
+
+ o = ss.option(form.Button, 'sysupgrade', _('Image'));
+ o.inputstyle = 'action important';
+ o.inputtitle = _('Flash image...');
+ o.onclick = L.bind(this.handleSysupgrade, this, storage_size);
+ }
+
+
+ s = m.section(form.NamedSection, 'config', 'config', _('Configuration'), _('This is a list of shell glob patterns for matching files and directories to include during sysupgrade. Modified files in /etc/config/ and certain other configurations are automatically preserved.'));
+ s.render = L.bind(function(view /*, ... */) {
+ return form.NamedSection.prototype.render.apply(this, this.varargs(arguments, 1))
+ .then(L.bind(function(node) {
+ node.appendChild(E('div', { 'class': 'cbi-page-actions' }, [
+ E('button', {
+ 'class': 'cbi-button cbi-button-save',
+ 'click': L.ui.createHandlerFn(view, 'handleBackupSave', this.map)
+ }, [ _('Save') ])
+ ]));
+
+ return node;
+ }, this));
+ }, s, this);
+
+ o = s.option(form.Button, 'showlist', _('Show current backup file list'));
+ o.inputstyle = 'action';
+ o.inputtitle = _('Open list...');
+ o.onclick = L.bind(this.handleBackupList, this);
+
+ o = s.option(form.TextValue, 'editlist');
+ o.forcewrite = true;
+ o.rows = 30;
+ o.load = function(section_id) {
+ return callFileRead('/etc/sysupgrade.conf');
+ };
+
+
+ return m.render();
+ },
+
+ handleSaveApply: null,
+ handleSave: null,
+ handleReset: null
+});
diff --git a/modules/luci-mod-system/luasrc/controller/admin/system.lua b/modules/luci-mod-system/luasrc/controller/admin/system.lua
index d6e1dc7815..c7d8ac5323 100644
--- a/modules/luci-mod-system/luasrc/controller/admin/system.lua
+++ b/modules/luci-mod-system/luasrc/controller/admin/system.lua
@@ -29,229 +29,12 @@ function index()
entry({"admin", "system", "leds"}, view("system/leds"), _("LED Configuration"), 60)
end
- entry({"admin", "system", "flashops"}, call("action_flashops"), _("Backup / Flash Firmware"), 70)
- entry({"admin", "system", "flashops", "reset"}, post("action_reset"))
- entry({"admin", "system", "flashops", "backup"}, post("action_backup"))
- entry({"admin", "system", "flashops", "backupmtdblock"}, post("action_backupmtdblock"))
- entry({"admin", "system", "flashops", "backupfiles"}, form("admin_system/backupfiles"))
-
- -- call() instead of post() due to upload handling!
- entry({"admin", "system", "flashops", "restore"}, call("action_restore"))
- entry({"admin", "system", "flashops", "sysupgrade"}, call("action_sysupgrade"))
+ entry({"admin", "system", "flash"}, view("system/flash"), _("Backup / Flash Firmware"), 70)
entry({"admin", "system", "reboot"}, template("admin_system/reboot"), _("Reboot"), 90)
entry({"admin", "system", "reboot", "call"}, post("action_reboot"))
end
-local function image_supported(image)
- return (os.execute("sysupgrade -T %q >/dev/null" % image) == 0)
-end
-
-local function image_checksum(image)
- return (luci.sys.exec("md5sum %q" % image):match("^([^%s]+)"))
-end
-
-local function image_sha256_checksum(image)
- return (luci.sys.exec("sha256sum %q" % image):match("^([^%s]+)"))
-end
-
-local function supports_sysupgrade()
- return nixio.fs.access("/lib/upgrade/platform.sh")
-end
-
-local function supports_reset()
- return (os.execute([[grep -sq "^overlayfs:/overlay / overlay " /proc/mounts]]) == 0)
-end
-
-local function storage_size()
- local size = 0
- if nixio.fs.access("/proc/mtd") then
- for l in io.lines("/proc/mtd") do
- local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
- if n == "linux" or n == "firmware" then
- size = tonumber(s, 16)
- break
- end
- end
- elseif nixio.fs.access("/proc/partitions") then
- for l in io.lines("/proc/partitions") do
- local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
- if b and n and not n:match('[0-9]') then
- size = tonumber(b) * 1024
- break
- end
- end
- end
- return size
-end
-
-
-function action_flashops()
- --
- -- Overview
- --
- luci.template.render("admin_system/flashops", {
- reset_avail = supports_reset(),
- upgrade_avail = supports_sysupgrade()
- })
-end
-
-function action_sysupgrade()
- local fs = require "nixio.fs"
- local http = require "luci.http"
- local image_tmp = "/tmp/firmware.img"
-
- local fp
- http.setfilehandler(
- function(meta, chunk, eof)
- if not fp and meta and meta.name == "image" then
- fp = io.open(image_tmp, "w")
- end
- if fp and chunk then
- fp:write(chunk)
- end
- if fp and eof then
- fp:close()
- end
- end
- )
-
- if not luci.dispatcher.test_post_security() then
- fs.unlink(image_tmp)
- return
- end
-
- --
- -- Cancel firmware flash
- --
- if http.formvalue("cancel") then
- fs.unlink(image_tmp)
- http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
- return
- end
-
- --
- -- Initiate firmware flash
- --
- local step = tonumber(http.formvalue("step")) or 1
- if step == 1 then
- local force = http.formvalue("force")
- if image_supported(image_tmp) or force then
- luci.template.render("admin_system/upgrade", {
- checksum = image_checksum(image_tmp),
- sha256ch = image_sha256_checksum(image_tmp),
- storage = storage_size(),
- size = (fs.stat(image_tmp, "size") or 0),
- keep = (not not http.formvalue("keep")),
- force = (not not http.formvalue("force"))
- })
- else
- fs.unlink(image_tmp)
- luci.template.render("admin_system/flashops", {
- reset_avail = supports_reset(),
- upgrade_avail = supports_sysupgrade(),
- image_invalid = true
- })
- end
-
- --
- -- Start sysupgrade flash
- --
- elseif step == 2 then
- local keep = (http.formvalue("keep") == "1") and "" or "-n"
- local force = (http.formvalue("force") == "1") and "-F" or ""
- luci.template.render("admin_system/applyreboot", {
- title = luci.i18n.translate("Flashing..."),
- msg = luci.i18n.translate("The system is flashing now.
DO NOT POWER OFF THE DEVICE!
Wait a few minutes before you try to reconnect. It might be necessary to renew the address of your computer to reach the device again, depending on your settings."),
- addr = (#keep > 0) and (#force > 0) and "192.168.1.1" or nil
- })
- luci.sys.process.exec({ "/bin/sh", "-c","sleep 1; killall dropbear uhttpd; sleep 1; /sbin/sysupgrade %s %s %q" %{ keep, force, image_tmp } }, nil, nil, true)
- end
-end
-
-function action_backup()
- luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"'
- %{ luci.sys.hostname(), os.date("%Y-%m-%d") })
-
- luci.http.prepare_content("application/x-targz")
- luci.sys.process.exec({ "/sbin/sysupgrade", "--create-backup", "-" }, luci.http.write)
-end
-
-function action_backupmtdblock()
- local mv = luci.http.formvalue("mtdblockname") or ""
- local m, n = mv:match('^([^%s%./"]+)/%d+/(%d+)$')
-
- if not m and n then
- luci.http.status(400, "Bad Request")
- return
- end
-
- luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s-%s.bin"'
- %{ luci.sys.hostname(), m, os.date("%Y-%m-%d") })
-
- luci.http.prepare_content("application/octet-stream")
- luci.sys.process.exec({ "/bin/dd", "if=/dev/mtd%s" % n, "conv=fsync,notrunc" }, luci.http.write)
-end
-
-function action_restore()
- local fs = require "nixio.fs"
- local http = require "luci.http"
- local archive_tmp = "/tmp/restore.tar.gz"
-
- local fp
- http.setfilehandler(
- function(meta, chunk, eof)
- if not fp and meta and meta.name == "archive" then
- fp = io.open(archive_tmp, "w")
- end
- if fp and chunk then
- fp:write(chunk)
- end
- if fp and eof then
- fp:close()
- end
- end
- )
-
- if not luci.dispatcher.test_post_security() then
- fs.unlink(archive_tmp)
- return
- end
-
- local upload = http.formvalue("archive")
- if upload and #upload > 0 then
- if os.execute("gunzip -t %q >/dev/null 2>&1" % archive_tmp) == 0 then
- luci.template.render("admin_system/applyreboot")
- os.execute("tar -C / -xzf %q >/dev/null 2>&1" % archive_tmp)
- luci.sys.reboot()
- else
- luci.template.render("admin_system/flashops", {
- reset_avail = supports_reset(),
- upgrade_avail = supports_sysupgrade(),
- backup_invalid = true
- })
- end
- return
- end
-
- http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
-end
-
-function action_reset()
- if supports_reset() then
- luci.template.render("admin_system/applyreboot", {
- title = luci.i18n.translate("Erasing..."),
- msg = luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."),
- addr = "192.168.1.1"
- })
-
- luci.sys.process.exec({ "/bin/sh", "-c", "sleep 1; killall dropbear uhttpd; sleep 1; jffs2reset -y && reboot" }, nil, nil, true)
- return
- end
-
- http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
-end
-
function action_reboot()
luci.sys.reboot()
end
diff --git a/modules/luci-mod-system/luasrc/view/admin_system/backupfiles.htm b/modules/luci-mod-system/luasrc/view/admin_system/backupfiles.htm
deleted file mode 100644
index c1f3361ae2..0000000000
--- a/modules/luci-mod-system/luasrc/view/admin_system/backupfiles.htm
+++ /dev/null
@@ -1,10 +0,0 @@
-<%#
- Copyright 2008 Steven Barth <%:Flash operations%>
-
-
-
-<%:Backup%>
- <%:Restore%>
- <%:Save mtdblock contents%>
- <%:Flash new firmware image%>
- <% if upgrade_avail then %>
-
- <% else %>
- <%:Flash Firmware%> - <%:Verify%>
-
- Click "Proceed" below to start the flash procedure. %>
-
- <% if storage > 0 and size > storage then %>
-
-
<%=checksum%>
<%=sha256ch%>