Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 2 | |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 3 | # TODO: openbmc/openbmc#2994 remove python 2 support |
| 4 | try: # python 2 |
| 5 | import gobject |
| 6 | except ImportError: # python 3 |
| 7 | from gi.repository import GObject as gobject |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 8 | import dbus |
| 9 | import dbus.service |
| 10 | import dbus.mainloop.glib |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 11 | import subprocess |
| 12 | import tempfile |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 13 | import shutil |
| 14 | import tarfile |
Norman James | 8a65a54 | 2016-02-03 19:04:44 -0600 | [diff] [blame] | 15 | import os |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 16 | from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 17 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 18 | DBUS_NAME = "org.openbmc.control.BmcFlash" |
| 19 | OBJ_NAME = "/org/openbmc/control/flash/bmc" |
| 20 | DOWNLOAD_INTF = "org.openbmc.managers.Download" |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 21 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 22 | BMC_DBUS_NAME = "xyz.openbmc_project.State.BMC" |
| 23 | BMC_OBJ_NAME = "/xyz/openbmc_project/state/bmc0" |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 24 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 25 | UPDATE_PATH = "/run/initramfs" |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 26 | |
| 27 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 28 | def doExtract(members, files): |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 29 | for tarinfo in members: |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 30 | if tarinfo.name in files: |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 31 | yield tarinfo |
| 32 | |
| 33 | |
Milton Miller | 3fc6b79 | 2017-08-15 06:57:14 +0000 | [diff] [blame] | 34 | def save_fw_env(): |
| 35 | fw_env = "/etc/fw_env.config" |
| 36 | lines = 0 |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 37 | files = [] |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 38 | envcfg = open(fw_env, "r") |
Milton Miller | 3fc6b79 | 2017-08-15 06:57:14 +0000 | [diff] [blame] | 39 | try: |
| 40 | for line in envcfg.readlines(): |
| 41 | # ignore lines that are blank or start with # |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 42 | if line.startswith("#"): |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 43 | continue |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 44 | if not len(line.strip()): |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 45 | continue |
| 46 | fn = line.partition("\t")[0] |
Milton Miller | 3fc6b79 | 2017-08-15 06:57:14 +0000 | [diff] [blame] | 47 | files.append(fn) |
| 48 | lines += 1 |
| 49 | finally: |
| 50 | envcfg.close() |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 51 | if lines < 1 or lines > 2 or (lines == 2 and files[0] != files[1]): |
| 52 | raise Exception("Error parsing %s\n" % fw_env) |
Milton Miller | 3fc6b79 | 2017-08-15 06:57:14 +0000 | [diff] [blame] | 53 | shutil.copyfile(files[0], os.path.join(UPDATE_PATH, "image-u-boot-env")) |
| 54 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 55 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 56 | class BmcFlashControl(DbusProperties, DbusObjectManager): |
| 57 | def __init__(self, bus, name): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 58 | super(BmcFlashControl, self).__init__(conn=bus, object_path=name) |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 59 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 60 | self.Set(DBUS_NAME, "status", "Idle") |
| 61 | self.Set(DBUS_NAME, "filename", "") |
| 62 | self.Set(DBUS_NAME, "preserve_network_settings", True) |
| 63 | self.Set(DBUS_NAME, "restore_application_defaults", False) |
| 64 | self.Set(DBUS_NAME, "update_kernel_and_apps", False) |
| 65 | self.Set(DBUS_NAME, "clear_persistent_files", False) |
| 66 | self.Set(DBUS_NAME, "auto_apply", False) |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 67 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 68 | bus.add_signal_receiver( |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 69 | self.download_error_handler, signal_name="DownloadError" |
| 70 | ) |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 71 | bus.add_signal_receiver( |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 72 | self.download_complete_handler, signal_name="DownloadComplete" |
| 73 | ) |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 74 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 75 | self.update_process = None |
| 76 | self.progress_name = None |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 77 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 78 | @dbus.service.method(DBUS_NAME, in_signature="ss", out_signature="") |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 79 | def updateViaTftp(self, ip, filename): |
| 80 | self.Set(DBUS_NAME, "status", "Downloading") |
| 81 | self.TftpDownload(ip, filename) |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 82 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 83 | @dbus.service.method(DBUS_NAME, in_signature="s", out_signature="") |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 84 | def update(self, filename): |
| 85 | self.Set(DBUS_NAME, "filename", filename) |
| 86 | self.download_complete_handler(filename, filename) |
Chris Austen | ea5b401 | 2016-05-20 16:41:40 -0500 | [diff] [blame] | 87 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 88 | @dbus.service.signal(DOWNLOAD_INTF, signature="ss") |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 89 | def TftpDownload(self, ip, filename): |
| 90 | self.Set(DBUS_NAME, "filename", filename) |
| 91 | pass |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 92 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 93 | # Signal handler |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 94 | def download_error_handler(self, filename): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 95 | if filename == self.Get(DBUS_NAME, "filename"): |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 96 | self.Set(DBUS_NAME, "status", "Download Error") |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 97 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 98 | def download_complete_handler(self, outfile, filename): |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 99 | # do update |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 100 | if filename != self.Get(DBUS_NAME, "filename"): |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 101 | return |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 102 | |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 103 | print("Download complete. Updating...") |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 104 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 105 | self.Set(DBUS_NAME, "status", "Download Complete") |
| 106 | copy_files = {} |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 107 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 108 | # determine needed files |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 109 | if not self.Get(DBUS_NAME, "update_kernel_and_apps"): |
| 110 | copy_files["image-bmc"] = True |
| 111 | else: |
| 112 | copy_files["image-kernel"] = True |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 113 | copy_files["image-rofs"] = True |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 114 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 115 | if self.Get(DBUS_NAME, "restore_application_defaults"): |
| 116 | copy_files["image-rwfs"] = True |
Norman James | 8a65a54 | 2016-02-03 19:04:44 -0600 | [diff] [blame] | 117 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 118 | # make sure files exist in archive |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 119 | try: |
| 120 | tar = tarfile.open(outfile, "r") |
| 121 | files = {} |
| 122 | for f in tar.getnames(): |
| 123 | files[f] = True |
| 124 | tar.close() |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 125 | for f in list(copy_files.keys()): |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 126 | if f not in files: |
| 127 | raise Exception( |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 128 | "ERROR: File not found in update archive: " + f |
| 129 | ) |
Milton Miller | b6cfc54 | 2016-06-17 11:43:40 -0500 | [diff] [blame] | 130 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 131 | except Exception as e: |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 132 | print(str(e)) |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 133 | self.Set(DBUS_NAME, "status", "Unpack Error") |
| 134 | return |
Milton Miller | b6cfc54 | 2016-06-17 11:43:40 -0500 | [diff] [blame] | 135 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 136 | try: |
| 137 | tar = tarfile.open(outfile, "r") |
| 138 | tar.extractall(UPDATE_PATH, members=doExtract(tar, copy_files)) |
| 139 | tar.close() |
Milton Miller | b6cfc54 | 2016-06-17 11:43:40 -0500 | [diff] [blame] | 140 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 141 | if self.Get(DBUS_NAME, "clear_persistent_files"): |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 142 | print("Removing persistent files") |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 143 | try: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 144 | os.unlink(UPDATE_PATH + "/whitelist") |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 145 | except OSError as e: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 146 | if e.errno == errno.EISDIR: |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 147 | pass |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 148 | elif e.errno == errno.ENOENT: |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 149 | pass |
| 150 | else: |
| 151 | raise |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 152 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 153 | try: |
| 154 | wldir = UPDATE_PATH + "/whitelist.d" |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 155 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 156 | for file in os.listdir(wldir): |
| 157 | os.unlink(os.path.join(wldir, file)) |
| 158 | except OSError as e: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 159 | if e.errno == errno.EISDIR: |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 160 | pass |
| 161 | else: |
| 162 | raise |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 163 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 164 | if self.Get(DBUS_NAME, "preserve_network_settings"): |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 165 | print("Preserving network settings") |
Milton Miller | 3fc6b79 | 2017-08-15 06:57:14 +0000 | [diff] [blame] | 166 | save_fw_env() |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 167 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 168 | except Exception as e: |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 169 | print(str(e)) |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 170 | self.Set(DBUS_NAME, "status", "Unpack Error") |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 171 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 172 | self.Verify() |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 173 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 174 | def Verify(self): |
| 175 | self.Set(DBUS_NAME, "status", "Checking Image") |
| 176 | try: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 177 | subprocess.check_call( |
| 178 | [ |
| 179 | "/run/initramfs/update", |
| 180 | "--no-flash", |
| 181 | "--no-save-files", |
| 182 | "--no-restore-files", |
| 183 | "--no-clean-saved-files", |
| 184 | ] |
| 185 | ) |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 186 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 187 | self.Set(DBUS_NAME, "status", "Image ready to apply.") |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 188 | if self.Get(DBUS_NAME, "auto_apply"): |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 189 | self.Apply() |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 190 | except Exception: |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 191 | self.Set(DBUS_NAME, "auto_apply", False) |
| 192 | try: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 193 | subprocess.check_output( |
| 194 | [ |
| 195 | "/run/initramfs/update", |
| 196 | "--no-flash", |
| 197 | "--ignore-mount", |
| 198 | "--no-save-files", |
| 199 | "--no-restore-files", |
| 200 | "--no-clean-saved-files", |
| 201 | ], |
| 202 | stderr=subprocess.STDOUT, |
| 203 | ) |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 204 | self.Set( |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 205 | DBUS_NAME, |
| 206 | "status", |
| 207 | "Deferred for mounted filesystem. reboot BMC to apply.", |
| 208 | ) |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 209 | except subprocess.CalledProcessError as e: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 210 | self.Set(DBUS_NAME, "status", "Verify error: %s" % e.output) |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 211 | except OSError as e: |
| 212 | self.Set( |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 213 | DBUS_NAME, |
| 214 | "status", |
| 215 | "Verify error: problem calling update: %s" % e.strerror, |
| 216 | ) |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 217 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 218 | def Cleanup(self): |
| 219 | if self.progress_name: |
| 220 | try: |
| 221 | os.unlink(self.progress_name) |
| 222 | self.progress_name = None |
| 223 | except oserror as e: |
| 224 | if e.errno == EEXIST: |
| 225 | pass |
| 226 | raise |
| 227 | self.update_process = None |
| 228 | self.Set(DBUS_NAME, "status", "Idle") |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 229 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 230 | @dbus.service.method(DBUS_NAME, in_signature="", out_signature="") |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 231 | def Abort(self): |
| 232 | if self.update_process: |
| 233 | try: |
| 234 | self.update_process.kill() |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 235 | except Exception: |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 236 | pass |
| 237 | for file in os.listdir(UPDATE_PATH): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 238 | if file.startswith("image-"): |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 239 | os.unlink(os.path.join(UPDATE_PATH, file)) |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 240 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 241 | self.Cleanup() |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 242 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 243 | @dbus.service.method(DBUS_NAME, in_signature="", out_signature="s") |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 244 | def GetUpdateProgress(self): |
| 245 | msg = "" |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 246 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 247 | if self.update_process and self.update_process.returncode is None: |
| 248 | self.update_process.poll() |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 249 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 250 | if self.update_process is None: |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 251 | pass |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 252 | elif self.update_process.returncode > 0: |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 253 | self.Set(DBUS_NAME, "status", "Apply failed") |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 254 | elif self.update_process.returncode is None: |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 255 | pass |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 256 | else: # (self.update_process.returncode == 0) |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 257 | files = "" |
| 258 | for file in os.listdir(UPDATE_PATH): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 259 | if file.startswith("image-"): |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 260 | files = files + file |
| 261 | if files == "": |
| 262 | msg = "Apply Complete. Reboot to take effect." |
| 263 | else: |
| 264 | msg = "Apply Incomplete, Remaining:" + files |
| 265 | self.Set(DBUS_NAME, "status", msg) |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 266 | |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 267 | msg = self.Get(DBUS_NAME, "status") + "\n" |
| 268 | if self.progress_name: |
| 269 | try: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 270 | prog = open(self.progress_name, "r") |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 271 | for line in prog: |
| 272 | # strip off initial sets of xxx\r here |
| 273 | # ignore crlf at the end |
| 274 | # cr will be -1 if no '\r' is found |
| 275 | cr = line.rfind("\r", 0, -2) |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 276 | msg = msg + line[(cr + 1):] |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 277 | except OSError as e: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 278 | if e.error == EEXIST: |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 279 | pass |
| 280 | raise |
| 281 | return msg |
| 282 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 283 | @dbus.service.method(DBUS_NAME, in_signature="", out_signature="") |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 284 | def Apply(self): |
| 285 | progress = None |
| 286 | self.Set(DBUS_NAME, "status", "Writing images to flash") |
| 287 | try: |
| 288 | progress = tempfile.NamedTemporaryFile( |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 289 | delete=False, prefix="progress." |
| 290 | ) |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 291 | self.progress_name = progress.name |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 292 | self.update_process = subprocess.Popen( |
| 293 | ["/run/initramfs/update"], |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 294 | stdout=progress.file, |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 295 | stderr=subprocess.STDOUT, |
| 296 | ) |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 297 | except Exception as e: |
| 298 | try: |
| 299 | progress.close() |
| 300 | os.unlink(progress.name) |
| 301 | self.progress_name = None |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 302 | except Exception: |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 303 | pass |
| 304 | raise |
| 305 | |
| 306 | try: |
| 307 | progress.close() |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 308 | except Exception: |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 309 | pass |
| 310 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 311 | @dbus.service.method(DBUS_NAME, in_signature="", out_signature="") |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 312 | def PrepareForUpdate(self): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 313 | subprocess.call( |
| 314 | [ |
| 315 | "fw_setenv", |
| 316 | "openbmconce", |
| 317 | "copy-files-to-ram copy-base-filesystem-to-ram", |
| 318 | ] |
| 319 | ) |
Adriana Kobylak | 7bef82c | 2017-10-02 19:33:48 -0500 | [diff] [blame] | 320 | # Set the variable twice so that it is written to both environments of |
| 321 | # the u-boot redundant environment variables since initramfs can only |
| 322 | # read one of the environments. |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 323 | subprocess.call( |
| 324 | [ |
| 325 | "fw_setenv", |
| 326 | "openbmconce", |
| 327 | "copy-files-to-ram copy-base-filesystem-to-ram", |
| 328 | ] |
| 329 | ) |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 330 | self.Set(DBUS_NAME, "status", "Switch to update mode in progress") |
| 331 | o = bus.get_object(BMC_DBUS_NAME, BMC_OBJ_NAME) |
Lei YU | 5a38763 | 2018-04-04 16:51:22 +0800 | [diff] [blame] | 332 | intf = dbus.Interface(o, "org.freedesktop.DBus.Properties") |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 333 | intf.Set( |
| 334 | BMC_DBUS_NAME, |
| 335 | "RequestedBMCTransition", |
| 336 | "xyz.openbmc_project.State.BMC.Transition.Reboot", |
| 337 | ) |
Milton Miller | 0c8c5d4 | 2016-06-29 17:41:36 -0500 | [diff] [blame] | 338 | |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 339 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 340 | if __name__ == "__main__": |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 341 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 342 | |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 343 | bus = get_dbus() |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 344 | obj = BmcFlashControl(bus, OBJ_NAME) |
| 345 | mainloop = gobject.MainLoop() |
Brad Bishop | f0f3efe | 2016-06-29 23:20:24 -0400 | [diff] [blame] | 346 | |
| 347 | obj.unmask_signals() |
Brad Bishop | 70852a3 | 2016-06-29 22:58:51 -0400 | [diff] [blame] | 348 | name = dbus.service.BusName(DBUS_NAME, bus) |
Brad Bishop | 34c3bf9 | 2016-08-30 20:34:51 -0400 | [diff] [blame] | 349 | |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 350 | print("Running Bmc Flash Control") |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 351 | mainloop.run() |
Brad Bishop | 5306675 | 2016-09-21 08:48:04 -0400 | [diff] [blame] | 352 | |
| 353 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |