10 #include <pb-config/pb-config.h>
11 #include <talloc/talloc.h>
12 #include <list/list.h>
14 #include <types/types.h>
15 #include <system/system.h>
16 #include <process/process.h>
19 #include "device-handler.h"
20 #include "discover-server.h"
27 struct device_handler {
28 struct discover_server *server;
31 struct discover_device **devices;
32 unsigned int n_devices;
34 struct waitset *waitset;
35 struct waiter *timeout_waiter;
36 bool autoboot_enabled;
37 unsigned int sec_to_boot;
39 struct discover_boot_option *default_boot_option;
40 struct list unresolved_boot_options;
43 static int mount_device(struct discover_device *dev);
44 static int umount_device(struct discover_device *dev);
46 void discover_context_add_boot_option(struct discover_context *ctx,
47 struct discover_boot_option *boot_option)
49 boot_option->source = ctx->parser;
50 list_add_tail(&ctx->boot_options, &boot_option->list);
51 talloc_steal(ctx, boot_option);
55 * device_handler_get_device_count - Get the count of current handler devices.
58 int device_handler_get_device_count(const struct device_handler *handler)
60 return handler->n_devices;
64 * device_handler_get_device - Get a handler device by index.
67 const struct discover_device *device_handler_get_device(
68 const struct device_handler *handler, unsigned int index)
70 if (index >= handler->n_devices) {
71 assert(0 && "bad index");
75 return handler->devices[index];
78 struct discover_boot_option *discover_boot_option_create(
79 struct discover_context *ctx,
80 struct discover_device *device)
82 struct discover_boot_option *opt;
84 opt = talloc_zero(ctx, struct discover_boot_option);
85 opt->option = talloc_zero(opt, struct boot_option);
91 static int device_match_uuid(struct discover_device *dev, const char *uuid)
93 return dev->uuid && !strcmp(dev->uuid, uuid);
96 static int device_match_label(struct discover_device *dev, const char *label)
98 return dev->label && !strcmp(dev->label, label);
101 static int device_match_id(struct discover_device *dev, const char *id)
103 return !strcmp(dev->device->id, id);
106 static struct discover_device *device_lookup(
107 struct device_handler *device_handler,
108 int (match_fn)(struct discover_device *, const char *),
111 struct discover_device *dev;
117 for (i = 0; i < device_handler->n_devices; i++) {
118 dev = device_handler->devices[i];
120 if (match_fn(dev, str))
127 struct discover_device *device_lookup_by_name(struct device_handler *handler,
130 if (!strncmp(name, "/dev/", strlen("/dev/")))
131 name += strlen("/dev/");
133 return device_lookup_by_id(handler, name);
136 struct discover_device *device_lookup_by_uuid(
137 struct device_handler *device_handler,
140 return device_lookup(device_handler, device_match_uuid, uuid);
143 struct discover_device *device_lookup_by_label(
144 struct device_handler *device_handler,
147 return device_lookup(device_handler, device_match_label, label);
150 struct discover_device *device_lookup_by_id(
151 struct device_handler *device_handler,
154 return device_lookup(device_handler, device_match_id, id);
157 void device_handler_destroy(struct device_handler *handler)
159 talloc_free(handler);
162 static int destroy_device(void *arg)
164 struct discover_device *dev = arg;
171 struct discover_device *discover_device_create(struct device_handler *handler,
174 struct discover_device *dev;
176 dev = device_lookup_by_id(handler, id);
180 dev = talloc_zero(handler, struct discover_device);
181 dev->device = talloc_zero(dev, struct device);
182 dev->device->id = talloc_strdup(dev->device, id);
183 list_init(&dev->params);
184 list_init(&dev->boot_options);
186 talloc_set_destructor(dev, destroy_device);
191 struct discover_device_param {
194 struct list_item list;
197 void discover_device_set_param(struct discover_device *device,
198 const char *name, const char *value)
200 struct discover_device_param *param;
203 list_for_each_entry(&device->params, param, list) {
204 if (!strcmp(param->name, name)) {
213 param = talloc(device, struct discover_device_param);
214 param->name = talloc_strdup(param, name);
215 list_add(&device->params, ¶m->list);
218 list_remove(¶m->list);
222 talloc_free(param->value);
225 param->value = talloc_strdup(param, value);
228 const char *discover_device_get_param(struct discover_device *device,
231 struct discover_device_param *param;
233 list_for_each_entry(&device->params, param, list) {
234 if (!strcmp(param->name, name))
240 struct device_handler *device_handler_init(struct discover_server *server,
241 struct waitset *waitset, int dry_run)
243 struct device_handler *handler;
245 handler = talloc_zero(NULL, struct device_handler);
246 handler->server = server;
247 handler->waitset = waitset;
248 handler->dry_run = dry_run;
249 handler->autoboot_enabled = config_get()->autoboot_enabled;
251 list_init(&handler->unresolved_boot_options);
253 /* set up our mount point base */
254 pb_mkdir_recursive(mount_base());
261 void device_handler_remove(struct device_handler *handler,
262 struct discover_device *device)
266 for (i = 0; i < handler->n_devices; i++)
267 if (handler->devices[i] == device)
270 if (i == handler->n_devices) {
275 handler->n_devices--;
276 memmove(&handler->devices[i], &handler->devices[i + 1],
277 (handler->n_devices - i) * sizeof(handler->devices[0]));
278 handler->devices = talloc_realloc(handler, handler->devices,
279 struct discover_device *, handler->n_devices);
281 if (device->notified)
282 discover_server_notify_device_remove(handler->server,
288 static void boot_status(void *arg, struct boot_status *status)
290 struct device_handler *handler = arg;
292 discover_server_notify_boot_status(handler->server, status);
295 static void countdown_status(struct device_handler *handler,
296 struct discover_boot_option *opt, unsigned int sec)
298 struct boot_status status;
300 status.type = BOOT_STATUS_INFO;
301 status.progress = -1;
302 status.detail = NULL;
303 status.message = talloc_asprintf(handler,
304 "Booting %s in %u sec", opt->option->name, sec);
306 discover_server_notify_boot_status(handler->server, &status);
308 talloc_free(status.message);
311 static int default_timeout(void *arg)
313 struct device_handler *handler = arg;
314 struct discover_boot_option *opt;
316 if (!handler->default_boot_option)
319 opt = handler->default_boot_option;
321 if (handler->sec_to_boot) {
322 countdown_status(handler, opt, handler->sec_to_boot);
323 handler->sec_to_boot--;
324 handler->timeout_waiter = waiter_register_timeout(
325 handler->waitset, 1000,
326 default_timeout, handler);
330 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
332 boot(handler, handler->default_boot_option, NULL,
333 handler->dry_run, boot_status, handler);
337 static bool priority_match(struct boot_priority *prio,
338 struct discover_boot_option *opt)
340 return prio->type == opt->device->device->type;
343 static int default_option_priority(struct discover_boot_option *opt)
345 const struct config *config;
346 struct boot_priority *prio;
349 config = config_get();
351 for (i = 0; i < config->n_boot_priorities; i++) {
352 prio = &config->boot_priorities[i];
353 if (priority_match(prio, opt))
360 static void set_default(struct device_handler *handler,
361 struct discover_boot_option *opt)
363 if (!handler->autoboot_enabled)
366 /* Resolve any conflicts: if we have a new default option, it only
367 * replaces the current if it has a higher priority. */
368 if (handler->default_boot_option) {
369 int new_prio, cur_prio;
371 new_prio = default_option_priority(opt);
372 cur_prio = default_option_priority(
373 handler->default_boot_option);
375 if (new_prio < cur_prio) {
376 handler->default_boot_option = opt;
377 /* extend the timeout a little, so the user sees some
378 * indication of the change */
379 handler->sec_to_boot += 2;
385 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
386 handler->default_boot_option = opt;
388 pb_log("Boot option %s set as default, timeout %u sec.\n",
389 opt->option->id, handler->sec_to_boot);
391 default_timeout(handler);
394 static bool resource_is_resolved(struct resource *res)
396 return !res || res->resolved;
399 /* We only use this in an assert, which will disappear if we're compiling
400 * with NDEBUG, so we need the 'used' attribute for these builds */
401 static bool __attribute__((used)) boot_option_is_resolved(
402 struct discover_boot_option *opt)
404 return resource_is_resolved(opt->boot_image) &&
405 resource_is_resolved(opt->initrd) &&
406 resource_is_resolved(opt->dtb) &&
407 resource_is_resolved(opt->icon);
410 static bool resource_resolve(struct resource *res, const char *name,
411 struct discover_boot_option *opt,
412 struct device_handler *handler)
414 struct parser *parser = opt->source;
416 if (resource_is_resolved(res))
419 pb_log("Attempting to resolve resource %s->%s with parser %s\n",
420 opt->option->id, name, parser->name);
421 parser->resolve_resource(handler, res);
423 return res->resolved;
426 static bool boot_option_resolve(struct discover_boot_option *opt,
427 struct device_handler *handler)
429 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
430 resource_resolve(opt->initrd, "initrd", opt, handler) &&
431 resource_resolve(opt->dtb, "dtb", opt, handler) &&
432 resource_resolve(opt->icon, "icon", opt, handler);
435 static void boot_option_finalise(struct device_handler *handler,
436 struct discover_boot_option *opt)
438 assert(boot_option_is_resolved(opt));
440 /* check that the parsers haven't set any of the final data */
441 assert(!opt->option->boot_image_file);
442 assert(!opt->option->initrd_file);
443 assert(!opt->option->dtb_file);
444 assert(!opt->option->icon_file);
445 assert(!opt->option->device_id);
448 opt->option->boot_image_file = opt->boot_image->url->full;
450 opt->option->initrd_file = opt->initrd->url->full;
452 opt->option->dtb_file = opt->dtb->url->full;
454 opt->option->icon_file = opt->icon->url->full;
456 opt->option->device_id = opt->device->device->id;
458 if (opt->option->is_default)
459 set_default(handler, opt);
462 static void notify_boot_option(struct device_handler *handler,
463 struct discover_boot_option *opt)
465 struct discover_device *dev = opt->device;
468 discover_server_notify_device_add(handler->server,
469 opt->device->device);
470 dev->notified = true;
471 discover_server_notify_boot_option_add(handler->server, opt->option);
474 static void process_boot_option_queue(struct device_handler *handler)
476 struct discover_boot_option *opt, *tmp;
478 list_for_each_entry_safe(&handler->unresolved_boot_options,
481 pb_log("queue: attempting resolution for %s\n",
484 if (!boot_option_resolve(opt, handler))
487 pb_log("\tresolved!\n");
489 list_remove(&opt->list);
490 list_add_tail(&opt->device->boot_options, &opt->list);
491 talloc_steal(opt->device, opt);
492 boot_option_finalise(handler, opt);
493 notify_boot_option(handler, opt);
497 struct discover_context *device_handler_discover_context_create(
498 struct device_handler *handler,
499 struct discover_device *device)
501 struct discover_context *ctx;
503 ctx = talloc(handler, struct discover_context);
504 ctx->device = device;
505 ctx->conf_url = NULL;
506 list_init(&ctx->boot_options);
512 * context_commit - Commit a temporary discovery context to the handler,
513 * and notify the clients about any new options / devices
515 void device_handler_discover_context_commit(struct device_handler *handler,
516 struct discover_context *ctx)
518 struct discover_device *dev = ctx->device;
519 struct discover_boot_option *opt, *tmp;
521 if (!device_lookup_by_id(handler, dev->device->id))
522 device_handler_add_device(handler, dev);
524 /* move boot options from the context to the device */
525 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
526 list_remove(&opt->list);
528 if (boot_option_resolve(opt, handler)) {
529 pb_log("boot option %s is resolved, "
530 "sending to clients\n",
532 list_add_tail(&dev->boot_options, &opt->list);
533 talloc_steal(dev, opt);
534 boot_option_finalise(handler, opt);
535 notify_boot_option(handler, opt);
537 if (!opt->source->resolve_resource) {
538 pb_log("parser %s gave us an unresolved "
539 "resource (%s), but no way to "
541 opt->source->name, opt->option->id);
544 pb_log("boot option %s is unresolved, "
547 list_add(&handler->unresolved_boot_options,
549 talloc_steal(handler, opt);
555 void device_handler_add_device(struct device_handler *handler,
556 struct discover_device *device)
558 handler->n_devices++;
559 handler->devices = talloc_realloc(handler, handler->devices,
560 struct discover_device *, handler->n_devices);
561 handler->devices[handler->n_devices - 1] = device;
565 /* Start discovery on a hotplugged device. The device will be in our devices
566 * array, but has only just been initialised by the hotplug source.
568 int device_handler_discover(struct device_handler *handler,
569 struct discover_device *dev, enum conf_method method)
571 struct discover_context *ctx;
573 process_boot_option_queue(handler);
575 /* create our context */
576 ctx = device_handler_discover_context_create(handler, dev);
580 /* run the parsers. This will populate the ctx's boot_option list. */
581 iterate_parsers(ctx, method);
583 /* add discovered stuff to the handler */
584 device_handler_discover_context_commit(handler, ctx);
591 /* incoming conf event */
592 int device_handler_conf(struct device_handler *handler,
593 struct discover_device *dev, struct pb_url *url,
594 enum conf_method method)
596 struct discover_context *ctx;
598 /* create our context */
599 ctx = device_handler_discover_context_create(handler, dev);
602 iterate_parsers(ctx, method);
604 device_handler_discover_context_commit(handler, ctx);
611 static struct discover_boot_option *find_boot_option_by_id(
612 struct device_handler *handler, const char *id)
616 for (i = 0; i < handler->n_devices; i++) {
617 struct discover_device *dev = handler->devices[i];
618 struct discover_boot_option *opt;
620 list_for_each_entry(&dev->boot_options, opt, list)
621 if (!strcmp(opt->option->id, id))
628 void device_handler_boot(struct device_handler *handler,
629 struct boot_command *cmd)
631 struct discover_boot_option *opt;
633 opt = find_boot_option_by_id(handler, cmd->option_id);
635 boot(handler, opt, cmd, handler->dry_run, boot_status, handler);
638 void device_handler_cancel_default(struct device_handler *handler)
640 struct boot_status status;
642 if (handler->timeout_waiter)
643 waiter_remove(handler->timeout_waiter);
645 handler->timeout_waiter = NULL;
646 handler->autoboot_enabled = false;
648 /* we only send status if we had a default boot option queued */
649 if (!handler->default_boot_option)
652 pb_log("Cancelling default boot option\n");
654 handler->default_boot_option = NULL;
656 status.type = BOOT_STATUS_INFO;
657 status.progress = -1;
658 status.detail = NULL;
659 status.message = "Default boot cancelled";
661 discover_server_notify_boot_status(handler->server, &status);
664 #ifndef PETITBOOT_TEST
665 static int mount_device(struct discover_device *dev)
669 if (!dev->device_path)
672 if (!dev->mount_path)
673 dev->mount_path = join_paths(dev, mount_base(),
676 if (pb_mkdir_recursive(dev->mount_path))
677 pb_log("couldn't create mount directory %s: %s\n",
678 dev->mount_path, strerror(errno));
680 rc = process_run_simple(dev, pb_system_apps.mount,
681 dev->device_path, dev->mount_path,
687 /* Retry mount without ro option. */
688 rc = process_run_simple(dev, pb_system_apps.mount,
689 dev->device_path, dev->mount_path, NULL);
694 pb_rmdir_recursive(mount_base(), dev->mount_path);
698 static int umount_device(struct discover_device *dev)
702 if (!dev->mount_path)
705 status = process_run_simple(dev, pb_system_apps.umount,
706 dev->mount_path, NULL);
708 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
711 pb_rmdir_recursive(mount_base(), dev->mount_path);
717 static int umount_device(struct discover_device *dev __attribute__((unused)))
722 static int __attribute__((unused)) mount_device(
723 struct discover_device *dev __attribute__((unused)))