2 * Copyright (C) 2009 Sony Computer Entertainment Inc.
3 * Copyright 2009 Sony Corp.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #if defined(HAVE_CONFIG_H)
26 #include <sys/socket.h>
27 #include <sys/types.h>
32 #include <types/types.h>
33 #include <talloc/talloc.h>
34 #include <waiter/waiter.h>
36 #include "device-handler.h"
39 #include "user-event.h"
42 #define MAC_ADDR_SIZE 6
43 #define IP_ADDR_SIZE 4
46 struct device_handler *handler;
50 static const char *event_action_name(enum event_action action)
53 case EVENT_ACTION_ADD:
55 case EVENT_ACTION_REMOVE:
57 case EVENT_ACTION_URL:
59 case EVENT_ACTION_DHCP:
61 case EVENT_ACTION_BOOT:
70 static void user_event_print_event(struct event __attribute__((unused)) *event)
74 pb_debug("user_event %s event:\n", event_action_name(event->action));
75 pb_debug("\tdevice: %s\n", event->device);
77 for (i = 0; i < event->n_params; i++)
78 pb_debug("\t%-12s => %s\n",
79 event->params[i].name, event->params[i].value);
82 static struct resource *user_event_resource(struct discover_boot_option *opt,
85 const char *siaddr, *boot_file;
90 siaddr = event_get_param(event, "siaddr");
92 pb_log("%s: next server option not found\n", __func__);
96 boot_file = event_get_param(event, "bootfile");
98 pb_log("%s: bootfile not found\n", __func__);
102 url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr, boot_file);
103 url = pb_url_parse(opt, url_str);
104 talloc_free(url_str);
109 res = create_url_resource(opt, url);
118 static int parse_user_event(struct discover_context *ctx, struct event *event)
120 struct discover_boot_option *d_opt;
121 char *server_ip, *root_dir, *p;
122 struct boot_option *opt;
126 dev = ctx->device->device;
128 d_opt = discover_boot_option_create(ctx, ctx->device);
134 val = event_get_param(event, "name");
137 pb_log("%s: no name found\n", __func__);
141 opt->id = talloc_asprintf(opt, "%s#%s", dev->id, val);
142 opt->name = talloc_strdup(opt, val);
144 d_opt->boot_image = user_event_resource(d_opt, event);
145 if (!d_opt->boot_image) {
146 pb_log("%s: no boot image found for %s!\n", __func__,
151 val = event_get_param(event, "rootpath");
153 server_ip = talloc_strdup(opt, val);
154 p = strchr(server_ip, ':');
156 root_dir = talloc_strdup(opt, p + 1);
158 opt->boot_args = talloc_asprintf(opt, "%s%s:%s",
159 "root=/dev/nfs ip=any nfsroot=",
160 server_ip, root_dir);
162 talloc_free(root_dir);
164 opt->boot_args = talloc_asprintf(opt, "%s",
165 "root=/dev/nfs ip=any nfsroot=");
168 talloc_free(server_ip);
171 opt->description = talloc_asprintf(opt, "%s %s", opt->boot_image_file,
172 opt->boot_args ? : "");
174 if (event_get_param(event, "default"))
175 opt->is_default = true;
177 discover_context_add_boot_option(ctx, d_opt);
187 static const char *parse_host_addr(struct event *event)
191 val = event_get_param(event, "tftp");
195 val = event_get_param(event, "siaddr");
199 val = event_get_param(event, "serverid");
206 static char *parse_mac_addr(struct discover_context *ctx, const char *mac)
208 unsigned int mac_addr_arr[MAC_ADDR_SIZE];
211 sscanf(mac, "%X:%X:%X:%X:%X:%X", mac_addr_arr, mac_addr_arr + 1,
212 mac_addr_arr + 2, mac_addr_arr + 3, mac_addr_arr + 4,
215 mac_addr = talloc_asprintf(ctx, "01-%02x-%02x-%02x-%02x-%02x-%02x",
216 mac_addr_arr[0], mac_addr_arr[1], mac_addr_arr[2],
217 mac_addr_arr[3], mac_addr_arr[4], mac_addr_arr[5]);
222 static char *parse_ip_addr(struct discover_context *ctx, const char *ip)
224 unsigned int ip_addr_arr[IP_ADDR_SIZE];
227 sscanf(ip, "%u.%u.%u.%u", ip_addr_arr, ip_addr_arr + 1,
228 ip_addr_arr + 2, ip_addr_arr + 3);
230 ip_hex = talloc_asprintf(ctx, "%02X%02X%02X%02X", ip_addr_arr[0],
231 ip_addr_arr[1], ip_addr_arr[2], ip_addr_arr[3]);
236 struct pb_url *user_event_parse_conf_url(struct discover_context *ctx,
237 struct event *event, bool *is_complete)
239 const char *conffile, *pathprefix, *host, *bootfile;
240 char *p, *basedir, *url_str;
243 conffile = event_get_param(event, "pxeconffile");
244 pathprefix = event_get_param(event, "pxepathprefix");
245 bootfile = event_get_param(event, "bootfile");
247 /* If we're given a conf file, we're able to generate a complete URL to
248 * the configuration file, and the parser doesn't need to do any
249 * further autodiscovery */
250 *is_complete = !!conffile;
252 /* if conffile is a URL, that's all we need */
253 if (conffile && is_url(conffile)) {
254 url = pb_url_parse(ctx, conffile);
258 /* If we can create a URL from pathprefix (optionally with
259 * conffile appended to create a complete URL), use that */
260 if (pathprefix && is_url(pathprefix)) {
262 url_str = talloc_asprintf(ctx, "%s%s",
263 pathprefix, conffile);
264 url = pb_url_parse(ctx, url_str);
265 talloc_free(url_str);
267 url = pb_url_parse(ctx, pathprefix);
273 host = parse_host_addr(event);
275 pb_log("%s: host address not found\n", __func__);
279 url_str = talloc_asprintf(ctx, "tftp://%s/", host);
281 /* if we have a pathprefix, use that directly.. */
283 /* strip leading slashes */
284 while (pathprefix[0] == '/')
286 url_str = talloc_asprintf_append(url_str, "%s", pathprefix);
288 /* ... otherwise, add a path based on the bootfile name, but only
289 * if conffile isn't an absolute path itself */
290 } else if (bootfile && !(conffile && conffile[0] == '/')) {
292 basedir = talloc_strdup(ctx, bootfile);
294 /* strip filename from the bootfile path, leaving only a
296 p = strrchr(basedir, '/');
302 url_str = talloc_asprintf_append(url_str, "%s/",
305 talloc_free(basedir);
308 /* finally, append conffile */
310 url_str = talloc_asprintf_append(url_str, "%s", conffile);
312 url = pb_url_parse(ctx, url_str);
314 talloc_free(url_str);
319 char **user_event_parse_conf_filenames(
320 struct discover_context *ctx, struct event *event)
322 char *mac_addr, *ip_hex;
323 const char *mac, *ip;
327 mac = event_get_param(event, "mac");
329 mac_addr = parse_mac_addr(ctx, mac);
333 ip = event_get_param(event, "ip");
335 ip_hex = parse_ip_addr(ctx, ip);
336 len = strlen(ip_hex);
342 if (!mac_addr && !ip_hex) {
343 pb_log("%s: neither mac nor ip parameter found\n", __func__);
347 /* Filenames as fallback IP's + mac + default */
348 filenames = talloc_array(ctx, char *, len + 3);
352 filenames[index++] = talloc_strdup(filenames, mac_addr);
355 filenames[index++] = talloc_strdup(filenames, ip_hex);
356 ip_hex[--len] = '\0';
359 filenames[index++] = talloc_strdup(filenames, "default");
360 filenames[index++] = NULL;
363 talloc_free(mac_addr);
371 static int user_event_dhcp(struct user_event *uev, struct event *event)
373 struct device_handler *handler = uev->handler;
374 struct discover_device *dev;
376 dev = discover_device_create(handler, event->device);
378 device_handler_dhcp(handler, dev, event);
383 static int user_event_conf(struct user_event *uev, struct event *event)
385 struct device_handler *handler = uev->handler;
386 struct discover_device *dev;
390 val = event_get_param(event, "url");
394 url = pb_url_parse(event, val);
398 dev = discover_device_create(handler, event->device);
400 device_handler_conf(handler, dev, url);
405 static int user_event_add(struct user_event *uev, struct event *event)
407 struct device_handler *handler = uev->handler;
408 struct discover_context *ctx;
409 struct discover_device *dev;
411 dev = discover_device_create(handler, event->device);
412 ctx = device_handler_discover_context_create(handler, dev);
414 parse_user_event(ctx, event);
416 device_handler_discover_context_commit(handler, ctx);
423 static int user_event_remove(struct user_event *uev, struct event *event)
425 struct device_handler *handler = uev->handler;
426 struct discover_device *dev;
428 dev = device_lookup_by_id(handler, event->device);
432 device_handler_remove(handler, dev);
437 static int user_event_url(struct user_event *uev, struct event *event)
439 struct device_handler *handler = uev->handler;
442 url = event_get_param(event, "url");
444 device_handler_process_url(handler, url, NULL, NULL);
449 static int user_event_boot(struct user_event *uev, struct event *event)
451 struct device_handler *handler = uev->handler;
452 struct boot_command *cmd = talloc(handler, struct boot_command);
454 cmd->option_id = talloc_strdup(cmd, event_get_param(event, "id"));
455 cmd->boot_image_file = talloc_strdup(cmd, event_get_param(event, "image"));
456 cmd->initrd_file = talloc_strdup(cmd, event_get_param(event, "initrd"));
457 cmd->dtb_file = talloc_strdup(cmd, event_get_param(event, "dtb"));
458 cmd->boot_args = talloc_strdup(cmd, event_get_param(event, "args"));
460 device_handler_boot(handler, cmd);
467 static void user_event_handle_message(struct user_event *uev, char *buf,
473 event = talloc(uev, struct event);
474 event->type = EVENT_TYPE_USER;
476 result = event_parse_ad_message(event, buf, len);
481 user_event_print_event(event);
483 switch (event->action) {
484 case EVENT_ACTION_ADD:
485 result = user_event_add(uev, event);
487 case EVENT_ACTION_REMOVE:
488 result = user_event_remove(uev, event);
490 case EVENT_ACTION_URL:
491 result = user_event_url(uev, event);
493 case EVENT_ACTION_CONF:
494 result = user_event_conf(uev, event);
496 case EVENT_ACTION_DHCP:
497 result = user_event_dhcp(uev, event);
499 case EVENT_ACTION_BOOT:
500 result = user_event_boot(uev, event);
511 static int user_event_process(void *arg)
513 struct user_event *uev = arg;
514 char buf[PBOOT_USER_EVENT_SIZE + 1];
517 len = recvfrom(uev->socket, buf, PBOOT_USER_EVENT_SIZE, 0, NULL, NULL);
520 pb_log("%s: socket read failed: %s", __func__, strerror(errno));
525 pb_log("%s: empty", __func__);
531 pb_debug("%s: %u bytes\n", __func__, len);
533 user_event_handle_message(uev, buf, len);
538 static int user_event_destructor(void *arg)
540 struct user_event *uev = arg;
542 pb_debug("%s\n", __func__);
544 if (uev->socket >= 0)
550 struct user_event *user_event_init(struct device_handler *handler,
551 struct waitset *waitset)
553 struct sockaddr_un addr;
554 struct user_event *uev;
556 unlink(PBOOT_USER_EVENT_SOCKET);
558 uev = talloc(handler, struct user_event);
560 uev->handler = handler;
562 uev->socket = socket(AF_UNIX, SOCK_DGRAM, 0);
563 if (uev->socket < 0) {
564 pb_log("%s: Error creating event handler socket: %s\n",
565 __func__, strerror(errno));
569 talloc_set_destructor(uev, user_event_destructor);
571 memset(&addr, 0, sizeof(addr));
572 addr.sun_family = AF_UNIX;
573 strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);
575 if (bind(uev->socket, (struct sockaddr *)&addr, sizeof(addr))) {
576 pb_log("Error binding event handler socket: %s\n",
580 waiter_register_io(waitset, uev->socket, WAIT_IN,
581 user_event_process, uev);
583 pb_debug("%s: waiting on %s\n", __func__, PBOOT_USER_EVENT_SOCKET);