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 ctx->test_data = NULL;
524 list_init(&ctx->boot_options);
530 * context_commit - Commit a temporary discovery context to the handler,
531 * and notify the clients about any new options / devices
533 void device_handler_discover_context_commit(struct device_handler *handler,
534 struct discover_context *ctx)
536 struct discover_device *dev = ctx->device;
537 struct discover_boot_option *opt, *tmp;
539 if (!device_lookup_by_id(handler, dev->device->id))
540 device_handler_add_device(handler, dev);
542 /* move boot options from the context to the device */
543 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
544 list_remove(&opt->list);
546 if (boot_option_resolve(opt, handler)) {
547 pb_log("boot option %s is resolved, "
548 "sending to clients\n",
550 list_add_tail(&dev->boot_options, &opt->list);
551 talloc_steal(dev, opt);
552 boot_option_finalise(handler, opt);
553 notify_boot_option(handler, opt);
555 if (!opt->source->resolve_resource) {
556 pb_log("parser %s gave us an unresolved "
557 "resource (%s), but no way to "
559 opt->source->name, opt->option->id);
562 pb_log("boot option %s is unresolved, "
565 list_add(&handler->unresolved_boot_options,
567 talloc_steal(handler, opt);
573 void device_handler_add_device(struct device_handler *handler,
574 struct discover_device *device)
576 handler->n_devices++;
577 handler->devices = talloc_realloc(handler, handler->devices,
578 struct discover_device *, handler->n_devices);
579 handler->devices[handler->n_devices - 1] = device;
583 /* Start discovery on a hotplugged device. The device will be in our devices
584 * array, but has only just been initialised by the hotplug source.
586 int device_handler_discover(struct device_handler *handler,
587 struct discover_device *dev, enum conf_method method)
589 struct discover_context *ctx;
592 process_boot_option_queue(handler);
594 /* create our context */
595 ctx = device_handler_discover_context_create(handler, dev);
597 rc = mount_device(dev);
601 /* run the parsers. This will populate the ctx's boot_option list. */
602 iterate_parsers(ctx, method);
604 /* add discovered stuff to the handler */
605 device_handler_discover_context_commit(handler, ctx);
613 /* incoming conf event */
614 int device_handler_conf(struct device_handler *handler,
615 struct discover_device *dev, struct pb_url *url,
616 enum conf_method method)
618 struct discover_context *ctx;
620 /* create our context */
621 ctx = device_handler_discover_context_create(handler, dev);
624 iterate_parsers(ctx, method);
626 device_handler_discover_context_commit(handler, ctx);
633 static struct discover_boot_option *find_boot_option_by_id(
634 struct device_handler *handler, const char *id)
638 for (i = 0; i < handler->n_devices; i++) {
639 struct discover_device *dev = handler->devices[i];
640 struct discover_boot_option *opt;
642 list_for_each_entry(&dev->boot_options, opt, list)
643 if (!strcmp(opt->option->id, id))
650 void device_handler_boot(struct device_handler *handler,
651 struct boot_command *cmd)
653 struct discover_boot_option *opt;
655 opt = find_boot_option_by_id(handler, cmd->option_id);
657 boot(handler, opt, cmd, handler->dry_run, boot_status, handler);
660 void device_handler_cancel_default(struct device_handler *handler)
662 struct boot_status status;
664 if (handler->timeout_waiter)
665 waiter_remove(handler->timeout_waiter);
667 handler->timeout_waiter = NULL;
668 handler->autoboot_enabled = false;
670 /* we only send status if we had a default boot option queued */
671 if (!handler->default_boot_option)
674 pb_log("Cancelling default boot option\n");
676 handler->default_boot_option = NULL;
678 status.type = BOOT_STATUS_INFO;
679 status.progress = -1;
680 status.detail = NULL;
681 status.message = "Default boot cancelled";
683 discover_server_notify_boot_status(handler->server, &status);
686 #ifndef PETITBOOT_TEST
687 static bool check_existing_mount(struct discover_device *dev)
689 struct stat devstat, mntstat;
694 rc = stat(dev->device_path, &devstat);
696 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
700 if (!S_ISBLK(devstat.st_mode)) {
701 pb_debug("%s: %s isn't a block device?\n", __func__,
706 fp = fopen("/proc/self/mounts", "r");
713 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
716 rc = stat(mnt->mnt_fsname, &mntstat);
720 if (!S_ISBLK(mntstat.st_mode))
723 if (mntstat.st_rdev == devstat.st_rdev) {
724 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
725 dev->mounted_rw = !!hasmntopt(mnt, "rw");
727 dev->unmount = false;
729 pb_debug("%s: %s is already mounted (r%c) at %s\n",
730 __func__, dev->device_path,
731 dev->mounted_rw ? 'w' : 'o',
742 static int mount_device(struct discover_device *dev)
746 if (!dev->device_path)
752 if (check_existing_mount(dev))
755 dev->mount_path = join_paths(dev, mount_base(),
758 if (pb_mkdir_recursive(dev->mount_path)) {
759 pb_log("couldn't create mount directory %s: %s\n",
760 dev->mount_path, strerror(errno));
764 rc = process_run_simple(dev, pb_system_apps.mount,
765 dev->device_path, dev->mount_path,
769 dev->mounted_rw = false;
774 /* Retry mount without ro option. */
775 rc = process_run_simple(dev, pb_system_apps.mount,
776 dev->device_path, dev->mount_path, NULL);
780 dev->mounted_rw = true;
785 pb_rmdir_recursive(mount_base(), dev->mount_path);
787 talloc_free(dev->mount_path);
788 dev->mount_path = NULL;
792 static int umount_device(struct discover_device *dev)
796 if (!dev->mounted || !dev->unmount)
799 status = process_run_simple(dev, pb_system_apps.umount,
800 dev->mount_path, NULL);
802 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
805 dev->mounted = false;
806 talloc_free(dev->mount_path);
807 dev->mount_path = NULL;
809 pb_rmdir_recursive(mount_base(), dev->mount_path);
814 int device_request_write(struct discover_device *dev, bool *release)
826 rc = process_run_simple(dev, pb_system_apps.mount, dev->mount_path,
827 "-o", "remount,rw", NULL);
831 dev->mounted_rw = true;
836 void device_release_write(struct discover_device *dev, bool release)
841 process_run_simple(dev, pb_system_apps.mount, dev->mount_path,
842 "-o", "remount,ro", NULL);
843 dev->mounted_rw = false;
848 static int umount_device(struct discover_device *dev __attribute__((unused)))
853 static int __attribute__((unused)) mount_device(
854 struct discover_device *dev __attribute__((unused)))
859 int device_request_write(struct discover_device *dev __attribute__((unused)),
866 void device_release_write(struct discover_device *dev __attribute__((unused)),
867 bool release __attribute__((unused)))