12 #include <pb-config/pb-config.h>
13 #include <talloc/talloc.h>
14 #include <list/list.h>
16 #include <types/types.h>
17 #include <system/system.h>
18 #include <process/process.h>
21 #include "device-handler.h"
22 #include "discover-server.h"
29 struct device_handler {
30 struct discover_server *server;
33 struct discover_device **devices;
34 unsigned int n_devices;
36 struct waitset *waitset;
37 struct waiter *timeout_waiter;
38 bool autoboot_enabled;
39 unsigned int sec_to_boot;
41 struct discover_boot_option *default_boot_option;
42 struct list unresolved_boot_options;
45 static int mount_device(struct discover_device *dev);
46 static int umount_device(struct discover_device *dev);
48 void discover_context_add_boot_option(struct discover_context *ctx,
49 struct discover_boot_option *boot_option)
51 boot_option->source = ctx->parser;
52 list_add_tail(&ctx->boot_options, &boot_option->list);
53 talloc_steal(ctx, boot_option);
57 * device_handler_get_device_count - Get the count of current handler devices.
60 int device_handler_get_device_count(const struct device_handler *handler)
62 return handler->n_devices;
66 * device_handler_get_device - Get a handler device by index.
69 const struct discover_device *device_handler_get_device(
70 const struct device_handler *handler, unsigned int index)
72 if (index >= handler->n_devices) {
73 assert(0 && "bad index");
77 return handler->devices[index];
80 struct discover_boot_option *discover_boot_option_create(
81 struct discover_context *ctx,
82 struct discover_device *device)
84 struct discover_boot_option *opt;
86 opt = talloc_zero(ctx, struct discover_boot_option);
87 opt->option = talloc_zero(opt, struct boot_option);
93 static int device_match_uuid(struct discover_device *dev, const char *uuid)
95 return dev->uuid && !strcmp(dev->uuid, uuid);
98 static int device_match_label(struct discover_device *dev, const char *label)
100 return dev->label && !strcmp(dev->label, label);
103 static int device_match_id(struct discover_device *dev, const char *id)
105 return !strcmp(dev->device->id, id);
108 static int device_match_serial(struct discover_device *dev, const char *serial)
110 const char *val = discover_device_get_param(dev, "ID_SERIAL");
111 return val && !strcmp(val, serial);
114 static struct discover_device *device_lookup(
115 struct device_handler *device_handler,
116 int (match_fn)(struct discover_device *, const char *),
119 struct discover_device *dev;
125 for (i = 0; i < device_handler->n_devices; i++) {
126 dev = device_handler->devices[i];
128 if (match_fn(dev, str))
135 struct discover_device *device_lookup_by_name(struct device_handler *handler,
138 if (!strncmp(name, "/dev/", strlen("/dev/")))
139 name += strlen("/dev/");
141 return device_lookup_by_id(handler, name);
144 struct discover_device *device_lookup_by_uuid(
145 struct device_handler *device_handler,
148 return device_lookup(device_handler, device_match_uuid, uuid);
151 struct discover_device *device_lookup_by_label(
152 struct device_handler *device_handler,
155 return device_lookup(device_handler, device_match_label, label);
158 struct discover_device *device_lookup_by_id(
159 struct device_handler *device_handler,
162 return device_lookup(device_handler, device_match_id, id);
165 struct discover_device *device_lookup_by_serial(
166 struct device_handler *device_handler,
169 return device_lookup(device_handler, device_match_serial, serial);
172 void device_handler_destroy(struct device_handler *handler)
174 talloc_free(handler);
177 static int destroy_device(void *arg)
179 struct discover_device *dev = arg;
186 struct discover_device *discover_device_create(struct device_handler *handler,
189 struct discover_device *dev;
191 dev = device_lookup_by_id(handler, id);
195 dev = talloc_zero(handler, struct discover_device);
196 dev->device = talloc_zero(dev, struct device);
197 dev->device->id = talloc_strdup(dev->device, id);
198 list_init(&dev->params);
199 list_init(&dev->boot_options);
201 talloc_set_destructor(dev, destroy_device);
206 struct discover_device_param {
209 struct list_item list;
212 void discover_device_set_param(struct discover_device *device,
213 const char *name, const char *value)
215 struct discover_device_param *param;
218 list_for_each_entry(&device->params, param, list) {
219 if (!strcmp(param->name, name)) {
228 param = talloc(device, struct discover_device_param);
229 param->name = talloc_strdup(param, name);
230 list_add(&device->params, ¶m->list);
233 list_remove(¶m->list);
237 talloc_free(param->value);
240 param->value = talloc_strdup(param, value);
243 const char *discover_device_get_param(struct discover_device *device,
246 struct discover_device_param *param;
248 list_for_each_entry(&device->params, param, list) {
249 if (!strcmp(param->name, name))
255 struct device_handler *device_handler_init(struct discover_server *server,
256 struct waitset *waitset, int dry_run)
258 struct device_handler *handler;
260 handler = talloc_zero(NULL, struct device_handler);
261 handler->server = server;
262 handler->waitset = waitset;
263 handler->dry_run = dry_run;
264 handler->autoboot_enabled = config_get()->autoboot_enabled;
266 list_init(&handler->unresolved_boot_options);
268 /* set up our mount point base */
269 pb_mkdir_recursive(mount_base());
276 void device_handler_remove(struct device_handler *handler,
277 struct discover_device *device)
281 for (i = 0; i < handler->n_devices; i++)
282 if (handler->devices[i] == device)
285 if (i == handler->n_devices) {
290 handler->n_devices--;
291 memmove(&handler->devices[i], &handler->devices[i + 1],
292 (handler->n_devices - i) * sizeof(handler->devices[0]));
293 handler->devices = talloc_realloc(handler, handler->devices,
294 struct discover_device *, handler->n_devices);
296 if (device->notified)
297 discover_server_notify_device_remove(handler->server,
303 static void boot_status(void *arg, struct boot_status *status)
305 struct device_handler *handler = arg;
307 discover_server_notify_boot_status(handler->server, status);
310 static void countdown_status(struct device_handler *handler,
311 struct discover_boot_option *opt, unsigned int sec)
313 struct boot_status status;
315 status.type = BOOT_STATUS_INFO;
316 status.progress = -1;
317 status.detail = NULL;
318 status.message = talloc_asprintf(handler,
319 "Booting %s in %u sec", opt->option->name, sec);
321 discover_server_notify_boot_status(handler->server, &status);
323 talloc_free(status.message);
326 static int default_timeout(void *arg)
328 struct device_handler *handler = arg;
329 struct discover_boot_option *opt;
331 if (!handler->default_boot_option)
334 opt = handler->default_boot_option;
336 if (handler->sec_to_boot) {
337 countdown_status(handler, opt, handler->sec_to_boot);
338 handler->sec_to_boot--;
339 handler->timeout_waiter = waiter_register_timeout(
340 handler->waitset, 1000,
341 default_timeout, handler);
345 handler->timeout_waiter = NULL;
347 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
349 boot(handler, handler->default_boot_option, NULL,
350 handler->dry_run, boot_status, handler);
354 static bool priority_match(struct boot_priority *prio,
355 struct discover_boot_option *opt)
357 return prio->type == opt->device->device->type;
360 static int default_option_priority(struct discover_boot_option *opt)
362 const struct config *config;
363 struct boot_priority *prio;
366 config = config_get();
368 for (i = 0; i < config->n_boot_priorities; i++) {
369 prio = &config->boot_priorities[i];
370 if (priority_match(prio, opt))
377 static void set_default(struct device_handler *handler,
378 struct discover_boot_option *opt)
380 if (!handler->autoboot_enabled)
383 /* Resolve any conflicts: if we have a new default option, it only
384 * replaces the current if it has a higher priority. */
385 if (handler->default_boot_option) {
386 int new_prio, cur_prio;
388 new_prio = default_option_priority(opt);
389 cur_prio = default_option_priority(
390 handler->default_boot_option);
392 if (new_prio < cur_prio) {
393 handler->default_boot_option = opt;
394 /* extend the timeout a little, so the user sees some
395 * indication of the change */
396 handler->sec_to_boot += 2;
402 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
403 handler->default_boot_option = opt;
405 pb_log("Boot option %s set as default, timeout %u sec.\n",
406 opt->option->id, handler->sec_to_boot);
408 default_timeout(handler);
411 static bool resource_is_resolved(struct resource *res)
413 return !res || res->resolved;
416 /* We only use this in an assert, which will disappear if we're compiling
417 * with NDEBUG, so we need the 'used' attribute for these builds */
418 static bool __attribute__((used)) boot_option_is_resolved(
419 struct discover_boot_option *opt)
421 return resource_is_resolved(opt->boot_image) &&
422 resource_is_resolved(opt->initrd) &&
423 resource_is_resolved(opt->dtb) &&
424 resource_is_resolved(opt->icon);
427 static bool resource_resolve(struct resource *res, const char *name,
428 struct discover_boot_option *opt,
429 struct device_handler *handler)
431 struct parser *parser = opt->source;
433 if (resource_is_resolved(res))
436 pb_log("Attempting to resolve resource %s->%s with parser %s\n",
437 opt->option->id, name, parser->name);
438 parser->resolve_resource(handler, res);
440 return res->resolved;
443 static bool boot_option_resolve(struct discover_boot_option *opt,
444 struct device_handler *handler)
446 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
447 resource_resolve(opt->initrd, "initrd", opt, handler) &&
448 resource_resolve(opt->dtb, "dtb", opt, handler) &&
449 resource_resolve(opt->icon, "icon", opt, handler);
452 static void boot_option_finalise(struct device_handler *handler,
453 struct discover_boot_option *opt)
455 assert(boot_option_is_resolved(opt));
457 /* check that the parsers haven't set any of the final data */
458 assert(!opt->option->boot_image_file);
459 assert(!opt->option->initrd_file);
460 assert(!opt->option->dtb_file);
461 assert(!opt->option->icon_file);
462 assert(!opt->option->device_id);
465 opt->option->boot_image_file = opt->boot_image->url->full;
467 opt->option->initrd_file = opt->initrd->url->full;
469 opt->option->dtb_file = opt->dtb->url->full;
471 opt->option->icon_file = opt->icon->url->full;
473 opt->option->device_id = opt->device->device->id;
475 if (opt->option->is_default)
476 set_default(handler, opt);
479 static void notify_boot_option(struct device_handler *handler,
480 struct discover_boot_option *opt)
482 struct discover_device *dev = opt->device;
485 discover_server_notify_device_add(handler->server,
486 opt->device->device);
487 dev->notified = true;
488 discover_server_notify_boot_option_add(handler->server, opt->option);
491 static void process_boot_option_queue(struct device_handler *handler)
493 struct discover_boot_option *opt, *tmp;
495 list_for_each_entry_safe(&handler->unresolved_boot_options,
498 pb_log("queue: attempting resolution for %s\n",
501 if (!boot_option_resolve(opt, handler))
504 pb_log("\tresolved!\n");
506 list_remove(&opt->list);
507 list_add_tail(&opt->device->boot_options, &opt->list);
508 talloc_steal(opt->device, opt);
509 boot_option_finalise(handler, opt);
510 notify_boot_option(handler, opt);
514 struct discover_context *device_handler_discover_context_create(
515 struct device_handler *handler,
516 struct discover_device *device)
518 struct discover_context *ctx;
520 ctx = talloc(handler, struct discover_context);
521 ctx->device = device;
522 ctx->conf_url = NULL;
523 list_init(&ctx->boot_options);
529 * context_commit - Commit a temporary discovery context to the handler,
530 * and notify the clients about any new options / devices
532 void device_handler_discover_context_commit(struct device_handler *handler,
533 struct discover_context *ctx)
535 struct discover_device *dev = ctx->device;
536 struct discover_boot_option *opt, *tmp;
538 if (!device_lookup_by_id(handler, dev->device->id))
539 device_handler_add_device(handler, dev);
541 /* move boot options from the context to the device */
542 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
543 list_remove(&opt->list);
545 if (boot_option_resolve(opt, handler)) {
546 pb_log("boot option %s is resolved, "
547 "sending to clients\n",
549 list_add_tail(&dev->boot_options, &opt->list);
550 talloc_steal(dev, opt);
551 boot_option_finalise(handler, opt);
552 notify_boot_option(handler, opt);
554 if (!opt->source->resolve_resource) {
555 pb_log("parser %s gave us an unresolved "
556 "resource (%s), but no way to "
558 opt->source->name, opt->option->id);
561 pb_log("boot option %s is unresolved, "
564 list_add(&handler->unresolved_boot_options,
566 talloc_steal(handler, opt);
572 void device_handler_add_device(struct device_handler *handler,
573 struct discover_device *device)
575 handler->n_devices++;
576 handler->devices = talloc_realloc(handler, handler->devices,
577 struct discover_device *, handler->n_devices);
578 handler->devices[handler->n_devices - 1] = device;
582 /* Start discovery on a hotplugged device. The device will be in our devices
583 * array, but has only just been initialised by the hotplug source.
585 int device_handler_discover(struct device_handler *handler,
586 struct discover_device *dev, enum conf_method method)
588 struct discover_context *ctx;
591 process_boot_option_queue(handler);
593 /* create our context */
594 ctx = device_handler_discover_context_create(handler, dev);
596 rc = mount_device(dev);
600 /* run the parsers. This will populate the ctx's boot_option list. */
601 iterate_parsers(ctx, method);
603 /* add discovered stuff to the handler */
604 device_handler_discover_context_commit(handler, ctx);
612 /* incoming conf event */
613 int device_handler_conf(struct device_handler *handler,
614 struct discover_device *dev, struct pb_url *url,
615 enum conf_method method)
617 struct discover_context *ctx;
619 /* create our context */
620 ctx = device_handler_discover_context_create(handler, dev);
623 iterate_parsers(ctx, method);
625 device_handler_discover_context_commit(handler, ctx);
632 static struct discover_boot_option *find_boot_option_by_id(
633 struct device_handler *handler, const char *id)
637 for (i = 0; i < handler->n_devices; i++) {
638 struct discover_device *dev = handler->devices[i];
639 struct discover_boot_option *opt;
641 list_for_each_entry(&dev->boot_options, opt, list)
642 if (!strcmp(opt->option->id, id))
649 void device_handler_boot(struct device_handler *handler,
650 struct boot_command *cmd)
652 struct discover_boot_option *opt;
654 opt = find_boot_option_by_id(handler, cmd->option_id);
656 boot(handler, opt, cmd, handler->dry_run, boot_status, handler);
659 void device_handler_cancel_default(struct device_handler *handler)
661 struct boot_status status;
663 if (handler->timeout_waiter)
664 waiter_remove(handler->timeout_waiter);
666 handler->timeout_waiter = NULL;
667 handler->autoboot_enabled = false;
669 /* we only send status if we had a default boot option queued */
670 if (!handler->default_boot_option)
673 pb_log("Cancelling default boot option\n");
675 handler->default_boot_option = NULL;
677 status.type = BOOT_STATUS_INFO;
678 status.progress = -1;
679 status.detail = NULL;
680 status.message = "Default boot cancelled";
682 discover_server_notify_boot_status(handler->server, &status);
685 #ifndef PETITBOOT_TEST
686 static bool check_existing_mount(struct discover_device *dev)
688 struct stat devstat, mntstat;
693 rc = stat(dev->device_path, &devstat);
695 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
699 if (!S_ISBLK(devstat.st_mode)) {
700 pb_debug("%s: %s isn't a block device?\n", __func__,
705 fp = fopen("/proc/self/mounts", "r");
712 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
715 rc = stat(mnt->mnt_fsname, &mntstat);
719 if (!S_ISBLK(mntstat.st_mode))
722 if (mntstat.st_rdev == devstat.st_rdev) {
723 pb_debug("%s: %s is already mounted at %s\n"
724 __func__, dev->device_path,
726 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
728 dev->unmount = false;
738 static int mount_device(struct discover_device *dev)
742 if (!dev->device_path)
748 if (check_existing_mount(dev))
751 dev->mount_path = join_paths(dev, mount_base(),
754 if (pb_mkdir_recursive(dev->mount_path)) {
755 pb_log("couldn't create mount directory %s: %s\n",
756 dev->mount_path, strerror(errno));
760 rc = process_run_simple(dev, pb_system_apps.mount,
761 dev->device_path, dev->mount_path,
769 /* Retry mount without ro option. */
770 rc = process_run_simple(dev, pb_system_apps.mount,
771 dev->device_path, dev->mount_path, NULL);
779 pb_rmdir_recursive(mount_base(), dev->mount_path);
781 talloc_free(dev->mount_path);
782 dev->mount_path = NULL;
786 static int umount_device(struct discover_device *dev)
790 if (!dev->mounted || !dev->unmount)
793 status = process_run_simple(dev, pb_system_apps.umount,
794 dev->mount_path, NULL);
796 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
799 dev->mounted = false;
800 talloc_free(dev->mount_path);
801 dev->mount_path = NULL;
803 pb_rmdir_recursive(mount_base(), dev->mount_path);
809 static int umount_device(struct discover_device *dev __attribute__((unused)))
814 static int __attribute__((unused)) mount_device(
815 struct discover_device *dev __attribute__((unused)))