Loading...
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | // SPDX-License-Identifier: GPL-2.0+ /* * Implementation of the logic to perform a boot * * Copyright 2025 Canonical Ltd * Written by Simon Glass <simon.glass@canonical.com> */ #define LOG_CATEGORY UCLASS_BOOTCTL #include <bootctl.h> #include <dm.h> #include <log.h> #include <time.h> #include <version.h> #include <bootctl/logic.h> #include <bootctl/measure.h> #include <bootctl/oslist.h> #include <bootctl/state.h> #include <bootctl/ui.h> #include <bootctl/util.h> #include <dm/device-internal.h> enum { COUNTDOWN_INTERVAL_MS = 1000, /* inteval between autoboot updates */ }; static int logic_prepare(struct udevice *dev) { struct logic_priv *priv = dev_get_priv(dev); int ret; /* figure out the UI to use */ ret = bootctl_get_dev(UCLASS_BOOTCTL_UI, &priv->ui); if (ret) { log_err("UI required but failed (err=%dE)\n", ret); return log_msg_ret("bgd", ret); } /* figure out the measurement to use */ if (priv->opt_measure) { ret = bootctl_get_dev(UCLASS_BOOTCTL_MEASURE, &priv->meas); if (ret) { log_err("Measurement required but failed (err=%dE)\n", ret); return log_msg_ret("bgs", ret); } } /* figure out at least one oslist driver to use */ ret = uclass_first_device_err(UCLASS_BOOTCTL_OSLIST, &priv->oslist); if (ret) return log_msg_ret("bgo", ret); /* figure out the state to use */ ret = bootctl_get_dev(UCLASS_BOOTCTL_STATE, &priv->state); if (ret) return log_msg_ret("bgs", ret); if (priv->opt_labels) { ret = bootdev_set_order(priv->opt_labels); if (ret) return log_msg_ret("blo", ret); } return 0; } static int logic_start(struct udevice *dev) { struct logic_priv *priv = dev_get_priv(dev); int ret; if (priv->opt_persist_state) { int ret; /* read in our state */ ret = bc_state_load(priv->state); if (ret) log_warning("Cannot load state, starting fresh (err=%dE)\n", ret); else priv->state_loaded = true; } ret = bc_ui_show(priv->ui); if (ret) { log_err("Cannot show display (err=%dE)\n", ret); return log_msg_ret("bds", ret); } priv->start_time = get_timer(0); if (priv->opt_autoboot) { priv->next_countdown = COUNTDOWN_INTERVAL_MS; priv->autoboot_remain_s = priv->opt_timeout; priv->autoboot_active = true; } if (priv->opt_default_os) bc_state_read_str(priv->state, "default", &priv->default_os); if (priv->opt_measure) { ret = bc_measure_start(priv->meas); if (ret) return log_msg_ret("pme", ret); } /* start scanning for OSes */ bc_oslist_setup_iter(&priv->iter); priv->scanning = true; return 0; } /** * prepare_for_boot() - Get ready to boot an OS * * Intended to include at least: * - A/B/recovery logic * - persist the state * - devicetree fix-up * - measure images * * @dev: Bootctrl logic device * @osinfo: OS to boot * Return: 0 if OK, -ve on error */ static int prepare_for_boot(struct udevice *dev, struct osinfo *osinfo) { struct logic_priv *priv = dev_get_priv(dev); int ret; if (priv->opt_track_success) { ret = bc_state_write_bool(priv->state, "recordfail", true); if (ret) log_warning("Cannot set up recordfail (err=%dE)\n", ret); } if (priv->opt_persist_state) { ret = bc_state_save(priv->state); if (ret) log_warning("Cannot save state (err=%dE)\n", ret); else priv->state_saved = true; } /* devicetree fix-ups go here */ /* measure loaded images */ if (priv->opt_measure) { struct alist result; ret = bc_measure_process(priv->meas, osinfo, &result); if (ret) return log_msg_ret("pbp", ret); show_measures(&result); /* TODO: pass these measurements on to OS */ } return 0; } /** * read_images() - Read all the images needed to boot an OS * * @dev: Bootctrl logic device * @osinfo: OS we intend to boot * Return: 0 if OK, -ve on error */ static int read_images(struct udevice *dev, struct osinfo *osinfo) { struct bootflow *bflow = &osinfo->bflow; int ret; ret = bootflow_read_all(bflow); if (ret) return log_msg_ret("rea", ret); log_debug("Images read: %d\n", bflow->images.count); return 0; } static int logic_poll(struct udevice *dev) { struct logic_priv *priv = dev_get_priv(dev); struct osinfo info; bool selected; int ret, seq; /* scan for the next OS, if any */ if (priv->scanning) { ret = bc_oslist_next(priv->oslist, &priv->iter, &info); if (!ret) { ret = bc_ui_add(priv->ui, &info); if (ret) return log_msg_ret("bda", ret); priv->refresh = true; } else { /* No more OSes from this driver, try the next */ ret = uclass_next_device_err(&priv->oslist); if (ret) priv->scanning = false; else memset(&priv->iter, '\0', sizeof(struct oslist_iter)); } } if (priv->autoboot_active && get_timer(priv->start_time) > priv->next_countdown) { ulong secs = get_timer(priv->start_time) / 1000; priv->autoboot_remain_s = secs >= priv->opt_timeout ? 0 : max(priv->opt_timeout - secs, 0ul); priv->next_countdown += COUNTDOWN_INTERVAL_MS; priv->refresh = true; } if (priv->refresh) { ret = bc_ui_render(priv->ui); if (ret) return log_msg_ret("bdr", ret); priv->refresh = false; } ret = bc_ui_poll(priv->ui, &seq, &selected); if (ret < 0) return log_msg_ret("bdo", ret); else if (ret) priv->refresh = true; if (!selected && priv->autoboot_active && !priv->autoboot_remain_s && seq >= 0) { log_info("Selecting %d due to timeout\n", seq); selected = true; } if (selected) { struct osinfo *os; os = alist_getw(&priv->osinfo, seq, struct osinfo); log_info("Selected %d: %s\n", seq, os->bflow.os_name); /* * try to read the images first; some methods don't support * this */ ret = read_images(dev, os); if (ret && ret != -ENOSYS) { if (ret) return log_msg_ret("lri", ret); } ret = prepare_for_boot(dev, os); if (ret) return log_msg_ret("lpb", ret); /* debugging */ // return -ESHUTDOWN; /* boot OS */ ret = bootflow_boot(&os->bflow); if (ret) log_warning("Boot failed (err=%dE)\n", ret); return -ESHUTDOWN; } return 0; } static int logic_of_to_plat(struct udevice *dev) { struct logic_priv *priv = dev_get_priv(dev); ofnode node = ofnode_find_subnode(dev_ofnode(dev), "options"); priv->opt_persist_state = ofnode_read_bool(node, "persist-state"); priv->opt_default_os = ofnode_read_bool(node, "default-os"); ofnode_read_u32(node, "timeout", &priv->opt_timeout); priv->opt_skip_timeout = ofnode_read_bool(node, "skip-timeout-on-success"); priv->opt_track_success = ofnode_read_bool(node, "track-success"); priv->opt_labels = ofnode_read_string(node, "labels"); priv->opt_autoboot = ofnode_read_bool(node, "autoboot"); priv->opt_measure = ofnode_read_bool(node, "measure"); return 0; } static int logic_probe(struct udevice *dev) { struct logic_priv *priv = dev_get_priv(dev); alist_init_struct(&priv->osinfo, struct osinfo); return 0; } static struct bc_logic_ops ops = { .prepare = logic_prepare, .start = logic_start, .poll = logic_poll, }; static const struct udevice_id logic_ids[] = { { .compatible = "bootctl,ubuntu-desktop" }, { .compatible = "bootctl,logic" }, { } }; U_BOOT_DRIVER(bc_logic) = { .name = "bc_logic", .id = UCLASS_BOOTCTL, .of_match = logic_ids, .ops = &ops, .of_to_plat = logic_of_to_plat, .probe = logic_probe, .priv_auto = sizeof(struct logic_priv), }; |