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 | // SPDX-License-Identifier: GPL-2.0+ /* * Uclass implementation for standard boot * * Copyright 2021 Google LLC * Written by Simon Glass <sjg@chromium.org> */ #define LOG_CATEGORY UCLASS_BOOTSTD #include <alist.h> #include <blk.h> #include <bootdev.h> #include <bootflow.h> #include <bootstd.h> #include <dm.h> #include <env.h> #include <log.h> #include <malloc.h> #include <dm/device-internal.h> #include <dm/lists.h> #include <dm/read.h> #include <dm/uclass-internal.h> DECLARE_GLOBAL_DATA_PTR; /* These are used if filename-prefixes is not present */ const char *const default_prefixes[] = {"/", "/boot/", NULL}; static int bootstd_of_to_plat(struct udevice *dev) { struct bootstd_priv *priv = dev_get_priv(dev); int ret; if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) { /* Don't check errors since livetree and flattree are different */ ret = dev_read_string_list(dev, "filename-prefixes", &priv->prefixes); dev_read_string_list(dev, "bootdev-order", &priv->bootdev_order); priv->theme = ofnode_find_subnode(dev_ofnode(dev), "theme"); } return 0; } static void bootstd_clear_glob_(struct bootstd_priv *priv) { struct bootflow *bflow; alist_for_each(bflow, &priv->bootflows) bootflow_remove(bflow); alist_empty(&priv->bootflows); } void bootstd_clear_glob(void) { struct bootstd_priv *std; if (bootstd_get_priv(&std)) return; bootstd_clear_glob_(std); } int bootstd_add_bootflow(struct bootflow *bflow) { struct bootstd_priv *std; int ret; ret = bootstd_get_priv(&std); if (ret) return ret; ret = std->bootflows.count; bflow = alist_add(&std->bootflows, *bflow); if (!bflow) return log_msg_ret("bf2", -ENOMEM); return ret; } int bootstd_clear_bootflows_for_bootdev(struct udevice *dev) { struct bootstd_priv *std = bootstd_try_priv(); struct bootflow *from, *to; /* if bootstd does not exist we cannot have any bootflows */ if (!std) return 0; /* Drop any bootflows that mention this dev */ alist_for_each_filter(from, to, &std->bootflows) { if (from->dev == dev) bootflow_remove(from); else *to++ = *from; } alist_update_end(&std->bootflows, to); return 0; } static int bootstd_remove(struct udevice *dev) { struct bootstd_priv *priv = dev_get_priv(dev); free(priv->prefixes); free(priv->bootdev_order); bootstd_clear_glob_(priv); return 0; } const char *const *const bootstd_get_bootdev_order(struct udevice *dev, bool *okp) { struct bootstd_priv *std = dev_get_priv(dev); const char *targets = env_get("boot_targets"); *okp = true; log_debug("- targets %s %p\n", targets, std->bootdev_order); if (targets && *targets) { str_free_list(std->env_order); std->env_order = str_to_list(targets); if (!std->env_order) { *okp = false; return NULL; } return std->env_order; } return std->bootdev_order; } void bootstd_set_bootdev_order(struct udevice *dev, const char **order_str) { struct bootstd_priv *std = dev_get_priv(dev); const char **name; free(std->bootdev_order); /* leak; convert to use alist */ std->bootdev_order = order_str; log_debug("bootdev_order:"); if (order_str) { for (name = order_str; *name; name++) log_debug(" %s", *name); } log_debug("\n"); } const char *const *const bootstd_get_prefixes(struct udevice *dev) { struct bootstd_priv *std = dev_get_priv(dev); return std->prefixes ? std->prefixes : default_prefixes; } struct bootstd_priv *bootstd_try_priv(void) { struct udevice *dev; dev = uclass_try_first_device(UCLASS_BOOTSTD); if (!dev || !device_active(dev)) return NULL; return dev_get_priv(dev); } int bootstd_get_priv(struct bootstd_priv **stdp) { struct udevice *dev; int ret; ret = uclass_first_device_err(UCLASS_BOOTSTD, &dev); if (ret) return ret; *stdp = dev_get_priv(dev); return 0; } int bootstd_img_add(struct blk_desc *desc, int part, const char *fname, enum bootflow_img_t type, ulong addr, ulong size) { struct udevice *bootdev = NULL; struct bootstd_priv *std; struct bootflow *bflow, bflow_s; int ret; ret = bootstd_get_priv(&std); if (ret) return ret; if (desc) { ret = bootdev_get_from_blk(desc->bdev, &bootdev); if (ret) return log_msg_ret("iad", ret); } bflow = alist_getw(&std->bootflows, std->adhoc_bflow, struct bootflow); if (!bflow) { bflow = &bflow_s; bootflow_init(bflow, bootdev, NULL); bflow->name = strdup("ad-hoc"); if (!bflow->name) return log_msg_ret("ian", -ENOMEM); } if (!bootflow_img_add(bflow, fname, type, addr, size)) return log_msg_ret("iaf", -ENOMEM); if (bflow == &bflow_s) { ret = bootstd_add_bootflow(bflow); if (ret < 0) return log_msg_ret("iab", ret); std->adhoc_bflow = ret; } return 0; } static int bootstd_probe(struct udevice *dev) { struct bootstd_priv *std = dev_get_priv(dev); alist_init_struct(&std->bootflows, struct bootflow); std->adhoc_bflow = -1; return 0; } /* For now, bind the bootmethod device if none are found in the devicetree */ int dm_scan_other(bool pre_reloc_only) { struct driver *drv = ll_entry_start(struct driver, driver); const int n_ents = ll_entry_count(struct driver, driver); struct udevice *dev, *bootstd; int i, ret; /* These are not needed before relocation */ if (!(gd->flags & GD_FLG_RELOC)) return 0; /* Create a bootstd device if needed */ uclass_find_first_device(UCLASS_BOOTSTD, &bootstd); if (!bootstd) { ret = device_bind_driver(gd->dm_root, "bootstd_drv", "bootstd", &bootstd); if (ret) return log_msg_ret("bootstd", ret); } /* If there are no bootmeth devices, create them */ uclass_find_first_device(UCLASS_BOOTMETH, &dev); if (dev) return 0; for (i = 0; i < n_ents; i++, drv++) { if (drv->id == UCLASS_BOOTMETH) { const char *name = drv->name; if (!strncmp("bootmeth_", name, 9)) name += 9; ret = device_bind(bootstd, drv, name, 0, ofnode_null(), &dev); if (ret) return log_msg_ret("meth", ret); } } return 0; } static const struct udevice_id bootstd_ids[] = { { .compatible = "u-boot,boot-std" }, { } }; U_BOOT_DRIVER(bootstd_drv) = { .id = UCLASS_BOOTSTD, .name = "bootstd_drv", .of_to_plat = bootstd_of_to_plat, .probe = bootstd_probe, .remove = bootstd_remove, .of_match = bootstd_ids, .priv_auto = sizeof(struct bootstd_priv), }; UCLASS_DRIVER(bootstd) = { .id = UCLASS_BOOTSTD, .name = "bootstd", #if CONFIG_IS_ENABLED(OF_REAL) .post_bind = dm_scan_fdt_dev, #endif }; |