]> git.ozlabs.org Git - petitboot/blob - lib/pb-protocol/pb-protocol.c
lib/efi: Add new routines to access efi variables
[petitboot] / lib / pb-protocol / pb-protocol.c
1
2 #include <assert.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <asm/byteorder.h>
8
9 #include <talloc/talloc.h>
10 #include <list/list.h>
11 #include <log/log.h>
12
13 #include "pb-protocol.h"
14
15
16 /* Message format:
17  *
18  * 4-byte action, determines the remaining message content
19  * 4-byte total payload len
20  *   - not including action and payload len header
21  *
22  * action = 0x1: device add message
23  *  payload:
24  *   4-byte len, id
25  *   1-byte type
26  *   4-byte len, name
27  *   4-byte len, description
28  *   4-byte len, icon_file
29  *
30  *   4-byte option count
31  *   for each option:
32  *    4-byte len, id
33  *    4-byte len, name
34  *    4-byte len, description
35  *    4-byte len, icon_file
36  *    4-byte len, boot_image_file
37  *    4-byte len, initrd_file
38  *    4-byte len, dtb_file
39  *    4-byte len, boot_args
40  *    4-byte len, args_sig_file
41  *
42  * action = 0x2: device remove message
43  *  payload:
44  *   4-byte len, id
45  *
46  * action = 0x3: boot
47  *  payload:
48  *   4-byte len, boot option id
49  *   4-byte len, boot_image_file
50  *   4-byte len, initrd_file
51  *   4-byte len, dtb_file
52  *   4-byte len, boot_args
53  *   4-byte len, args_sig_file
54  *
55  */
56
57 void pb_protocol_dump_device(const struct device *dev, const char *text,
58         FILE *stream)
59 {
60         struct boot_option *opt;
61
62         fprintf(stream, "%snew dev:\n", text);
63         fprintf(stream, "%s\tid:   %s\n", text, dev->id);
64         fprintf(stream, "%s\tname: %s\n", text, dev->name);
65         fprintf(stream, "%s\tdesc: %s\n", text, dev->description);
66         fprintf(stream, "%s\ticon: %s\n", text, dev->icon_file);
67         fprintf(stream, "%s\tboot options:\n", text);
68         list_for_each_entry(&dev->boot_options, opt, list) {
69                 fprintf(stream, "%s\t\tid:   %s\n", text, opt->id);
70                 fprintf(stream, "%s\t\tname: %s\n", text, opt->name);
71                 fprintf(stream, "%s\t\tdesc: %s\n", text, opt->description);
72                 fprintf(stream, "%s\t\ticon: %s\n", text, opt->icon_file);
73                 fprintf(stream, "%s\t\tboot: %s\n", text, opt->boot_image_file);
74                 fprintf(stream, "%s\t\tinit: %s\n", text, opt->initrd_file);
75                 fprintf(stream, "%s\t\tdtb:  %s\n", text, opt->dtb_file);
76                 fprintf(stream, "%s\t\targs: %s\n", text, opt->boot_args);
77                 fprintf(stream, "%s\t\tasig: %s\n", text, opt->args_sig_file);
78                 fprintf(stream, "%s\t\ttype: %s\n", text,
79                         opt->type == DISCOVER_BOOT_OPTION ? "boot" : "plugin");
80         }
81 }
82
83 int pb_protocol_device_cmp(const struct device *a, const struct device *b)
84 {
85         return !strcmp(a->id, b->id);
86 }
87
88 int pb_protocol_boot_option_cmp(const struct boot_option *a,
89         const struct boot_option *b)
90 {
91         return !strcmp(a->id, b->id);
92 }
93
94 /* Write a string into the buffer, starting at pos.
95  *
96  * Returns the total length used for the write, including length header.
97  */
98 int pb_protocol_serialise_string(char *pos, const char *str)
99 {
100         int len = 0;
101
102         if (str)
103                 len = strlen(str);
104
105         *(uint32_t *)pos = __cpu_to_be32(len);
106         pos += sizeof(uint32_t);
107
108         memcpy(pos, str, len);
109
110         return len + sizeof(uint32_t);
111 }
112
113 /* Read a string from a buffer, allocating the new string as necessary.
114  *
115  * @param[in] ctx       The talloc context to base the allocation on
116  * @param[in,out] pos   Where to start reading
117  * @param[in,out] len   The amount of data remaining in the buffer
118  * @param[out] str      Pointer to resuling string
119  * @return              zero on success, non-zero on failure
120  */
121 static int read_string(void *ctx, const char **pos, unsigned int *len,
122         char **str)
123 {
124         uint32_t str_len, read_len;
125
126         if (*len < sizeof(uint32_t))
127                 return -1;
128
129         str_len = __be32_to_cpu(*(uint32_t *)(*pos));
130         read_len = sizeof(uint32_t);
131
132         if (read_len + str_len > *len)
133                 return -1;
134
135         if (str_len == 0)
136                 *str = NULL;
137         else
138                 *str = talloc_strndup(ctx, *pos + read_len, str_len);
139
140         read_len += str_len;
141
142         /* all ok, update the caller's pointers */
143         *pos += read_len;
144         *len -= read_len;
145
146         return 0;
147 }
148
149 static int read_u32(const char **pos, unsigned int *len, unsigned int *p)
150 {
151         if (*len < sizeof(uint32_t))
152                 return -1;
153
154         *p = (unsigned int)__be32_to_cpu(*(uint32_t *)(*pos));
155         *pos += sizeof(uint32_t);
156         *len -= sizeof(uint32_t);
157
158         return 0;
159 }
160
161 char *pb_protocol_deserialise_string(void *ctx,
162                 const struct pb_protocol_message *message)
163 {
164         const char *buf;
165         char *str;
166         unsigned int len;
167
168         len = message->payload_len;
169         buf = message->payload;
170
171         if (read_string(ctx, &buf, &len, &str))
172                 return NULL;
173
174         return str;
175 }
176
177 static int optional_strlen(const char *str)
178 {
179         if (!str)
180                 return 0;
181         return strlen(str);
182 }
183
184 int pb_protocol_device_len(const struct device *dev)
185 {
186         return  4 + optional_strlen(dev->id) +
187                 sizeof(dev->type) +
188                 4 + optional_strlen(dev->name) +
189                 4 + optional_strlen(dev->description) +
190                 4 + optional_strlen(dev->icon_file);
191 }
192
193 int pb_protocol_boot_option_len(const struct boot_option *opt)
194 {
195
196         return  4 + optional_strlen(opt->device_id) +
197                 4 + optional_strlen(opt->id) +
198                 4 + optional_strlen(opt->name) +
199                 4 + optional_strlen(opt->description) +
200                 4 + optional_strlen(opt->icon_file) +
201                 4 + optional_strlen(opt->boot_image_file) +
202                 4 + optional_strlen(opt->initrd_file) +
203                 4 + optional_strlen(opt->dtb_file) +
204                 4 + optional_strlen(opt->boot_args) +
205                 4 + optional_strlen(opt->args_sig_file) +
206                 sizeof(opt->is_default) +
207                 sizeof(opt->type);
208 }
209
210 int pb_protocol_boot_len(const struct boot_command *boot)
211 {
212         return  4 + optional_strlen(boot->option_id) +
213                 4 + optional_strlen(boot->boot_image_file) +
214                 4 + optional_strlen(boot->initrd_file) +
215                 4 + optional_strlen(boot->dtb_file) +
216                 4 + optional_strlen(boot->boot_args) +
217                 4 + optional_strlen(boot->args_sig_file) +
218                 4 + optional_strlen(boot->console);
219 }
220
221 int pb_protocol_boot_status_len(const struct status *status)
222 {
223         return  4 +     /* type */
224                 4 + optional_strlen(status->message) +
225                 4 +     /* backlog */
226                 4;
227 }
228
229 int pb_protocol_system_info_len(const struct system_info *sysinfo)
230 {
231         unsigned int len, i;
232
233         len =   4 + optional_strlen(sysinfo->type) +
234                 4 + optional_strlen(sysinfo->identifier) +
235                 4 + 4;
236
237         len +=  4;
238         for (i = 0; i < sysinfo->n_primary; i++)
239                 len += 4 + optional_strlen(sysinfo->platform_primary[i]);
240         len +=  4;
241         for (i = 0; i < sysinfo->n_other; i++)
242                 len += 4 + optional_strlen(sysinfo->platform_other[i]);
243
244         len +=  4;
245         for (i = 0; i < sysinfo->n_bmc_current; i++)
246                 len += 4 + optional_strlen(sysinfo->bmc_current[i]);
247         len +=  4;
248         for (i = 0; i < sysinfo->n_bmc_golden; i++)
249                 len += 4 + optional_strlen(sysinfo->bmc_golden[i]);
250
251         for (i = 0; i < sysinfo->n_interfaces; i++) {
252                 struct interface_info *if_info = sysinfo->interfaces[i];
253                 len +=  4 + if_info->hwaddr_size +
254                         4 + optional_strlen(if_info->name) +
255                         sizeof(if_info->link) +
256                         4 + optional_strlen(if_info->address) +
257                         4 + optional_strlen(if_info->address_v6);
258         }
259
260         for (i = 0; i < sysinfo->n_blockdevs; i++) {
261                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
262                 len +=  4 + optional_strlen(bd_info->name) +
263                         4 + optional_strlen(bd_info->uuid) +
264                         4 + optional_strlen(bd_info->mountpoint);
265         }
266
267         /* BMC MAC */
268         len += HWADDR_SIZE;
269
270         return len;
271 }
272
273 static int pb_protocol_interface_config_len(struct interface_config *conf)
274 {
275         unsigned int len;
276
277         len =   sizeof(conf->hwaddr) +
278                 4 /* conf->ignore */;
279
280         if (conf->ignore)
281                 return len;
282
283         len += 4 /* conf->method */;
284
285         if (conf->method == CONFIG_METHOD_STATIC) {
286                 len += 4 + optional_strlen(conf->static_config.address);
287                 len += 4 + optional_strlen(conf->static_config.gateway);
288                 len += 4 + optional_strlen(conf->static_config.url);
289         }
290
291         len += 4 /* conf->override */;
292
293         return len;
294 }
295
296 int pb_protocol_config_len(const struct config *config)
297 {
298         unsigned int i, len;
299
300         len =   4 /* config->autoboot_enabled */ +
301                 4 /* config->autoboot_timeout_sec */ +
302                 4 /* config->safe_mode */;
303
304         len += 4;
305         for (i = 0; i < config->network.n_interfaces; i++)
306                 len += pb_protocol_interface_config_len(
307                                 config->network.interfaces[i]);
308
309         len += 4;
310         for (i = 0; i < config->network.n_dns_servers; i++)
311                 len += 4 + optional_strlen(config->network.dns_servers[i]);
312
313         len += 4 + optional_strlen(config->http_proxy);
314         len += 4 + optional_strlen(config->https_proxy);
315
316         len += 4;
317         for (i = 0; i < config->n_autoboot_opts; i++) {
318                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
319                         len += 4 + 4;
320                 else
321                         len += 4 + 4 +
322                                 optional_strlen(config->autoboot_opts[i].uuid);
323         }
324
325         len += 4 + 4; /* ipmi_bootdev, ipmi_bootdev_persistent */
326
327         len += 4; /* allow_writes */
328
329         len += 4; /* n_consoles */
330         for (i = 0; i < config->n_consoles; i++)
331                 len += 4 + optional_strlen(config->consoles[i]);
332
333         len += 4 + optional_strlen(config->boot_console);
334         len += 4; /* manual_console */
335
336         len += 4 + optional_strlen(config->lang);
337
338         return len;
339 }
340
341 int pb_protocol_url_len(const char *url)
342 {
343         /* url + length field */
344         return 4 + optional_strlen(url);
345 }
346
347
348 int pb_protocol_plugin_option_len(const struct plugin_option *opt)
349 {
350         unsigned int i, len = 0;
351
352         len += 4 + optional_strlen(opt->id);
353         len += 4 + optional_strlen(opt->name);
354         len += 4 + optional_strlen(opt->vendor);
355         len += 4 + optional_strlen(opt->vendor_id);
356         len += 4 + optional_strlen(opt->version);
357         len += 4 + optional_strlen(opt->date);
358         len += 4 + optional_strlen(opt->plugin_file);
359
360         len += 4; /* n_executables */
361         for (i = 0; i < opt->n_executables; i++)
362                 len += 4 + optional_strlen(opt->executables[i]);
363
364         return len;
365 }
366
367 int pb_protocol_temp_autoboot_len(const struct autoboot_option *opt)
368 {
369         unsigned int len = 0;
370
371         /* boot_type */
372         len += 4;
373
374         if (opt->boot_type == BOOT_DEVICE_TYPE)
375                 len += 4;
376         else
377                 len += optional_strlen(opt->uuid);
378
379         return len;
380 }
381
382 int pb_protocol_serialise_device(const struct device *dev,
383                 char *buf, int buf_len)
384 {
385         char *pos = buf;
386
387         pos += pb_protocol_serialise_string(pos, dev->id);
388         *(enum device_type *)pos = dev->type;
389         pos += sizeof(enum device_type);
390         pos += pb_protocol_serialise_string(pos, dev->name);
391         pos += pb_protocol_serialise_string(pos, dev->description);
392         pos += pb_protocol_serialise_string(pos, dev->icon_file);
393
394         assert(pos <= buf + buf_len);
395         (void)buf_len;
396
397         return 0;
398 }
399
400 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
401                 char *buf, int buf_len)
402 {
403         char *pos = buf;
404
405         pos += pb_protocol_serialise_string(pos, opt->device_id);
406         pos += pb_protocol_serialise_string(pos, opt->id);
407         pos += pb_protocol_serialise_string(pos, opt->name);
408         pos += pb_protocol_serialise_string(pos, opt->description);
409         pos += pb_protocol_serialise_string(pos, opt->icon_file);
410         pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
411         pos += pb_protocol_serialise_string(pos, opt->initrd_file);
412         pos += pb_protocol_serialise_string(pos, opt->dtb_file);
413         pos += pb_protocol_serialise_string(pos, opt->boot_args);
414         pos += pb_protocol_serialise_string(pos, opt->args_sig_file);
415
416         *(bool *)pos = opt->is_default;
417         pos += sizeof(bool);
418
419         *(uint32_t *)pos = __cpu_to_be32(opt->type);
420         pos += 4;
421
422         assert(pos <= buf + buf_len);
423         (void)buf_len;
424
425         return 0;
426 }
427
428 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
429                 char *buf, int buf_len)
430 {
431         char *pos = buf;
432
433         pos += pb_protocol_serialise_string(pos, boot->option_id);
434         pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
435         pos += pb_protocol_serialise_string(pos, boot->initrd_file);
436         pos += pb_protocol_serialise_string(pos, boot->dtb_file);
437         pos += pb_protocol_serialise_string(pos, boot->boot_args);
438         pos += pb_protocol_serialise_string(pos, boot->args_sig_file);
439         pos += pb_protocol_serialise_string(pos, boot->console);
440
441         assert(pos <= buf + buf_len);
442         (void)buf_len;
443
444         return 0;
445 }
446
447 int pb_protocol_serialise_boot_status(const struct status *status,
448                 char *buf, int buf_len)
449 {
450         char *pos = buf;
451
452         *(uint32_t *)pos = __cpu_to_be32(status->type);
453         pos += sizeof(uint32_t);
454
455         pos += pb_protocol_serialise_string(pos, status->message);
456
457         *(bool *)pos = __cpu_to_be32(status->backlog);
458         pos += sizeof(bool);
459
460         assert(pos <= buf + buf_len);
461         (void)buf_len;
462
463         return 0;
464 }
465
466 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
467                 char *buf, int buf_len)
468 {
469         char *pos = buf;
470         unsigned int i;
471
472         pos += pb_protocol_serialise_string(pos, sysinfo->type);
473         pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
474
475         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_primary);
476         pos += sizeof(uint32_t);
477         for (i = 0; i < sysinfo->n_primary; i++)
478                 pos += pb_protocol_serialise_string(pos, sysinfo->platform_primary[i]);
479
480         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_other);
481         pos += sizeof(uint32_t);
482         for (i = 0; i < sysinfo->n_other; i++)
483                 pos += pb_protocol_serialise_string(pos, sysinfo->platform_other[i]);
484
485         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_current);
486         pos += sizeof(uint32_t);
487         for (i = 0; i < sysinfo->n_bmc_current; i++)
488                 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_current[i]);
489
490         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_golden);
491         pos += sizeof(uint32_t);
492         for (i = 0; i < sysinfo->n_bmc_golden; i++)
493                 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_golden[i]);
494
495         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
496         pos += sizeof(uint32_t);
497
498         for (i = 0; i < sysinfo->n_interfaces; i++) {
499                 struct interface_info *if_info = sysinfo->interfaces[i];
500
501                 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
502                 pos += sizeof(uint32_t);
503
504                 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
505                 pos += if_info->hwaddr_size;
506
507                 pos += pb_protocol_serialise_string(pos, if_info->name);
508
509                 *(bool *)pos = if_info->link;
510                 pos += sizeof(bool);
511
512                 pos += pb_protocol_serialise_string(pos, if_info->address);
513                 pos += pb_protocol_serialise_string(pos, if_info->address_v6);
514         }
515
516         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
517         pos += sizeof(uint32_t);
518
519         for (i = 0; i < sysinfo->n_blockdevs; i++) {
520                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
521
522                 pos += pb_protocol_serialise_string(pos, bd_info->name);
523                 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
524                 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
525         }
526
527         if (sysinfo->bmc_mac)
528                 memcpy(pos, sysinfo->bmc_mac, HWADDR_SIZE);
529         else
530                 memset(pos, 0, HWADDR_SIZE);
531         pos += HWADDR_SIZE;
532
533         assert(pos <= buf + buf_len);
534         (void)buf_len;
535
536         return 0;
537 }
538
539 static int pb_protocol_serialise_config_interface(char *buf,
540                 struct interface_config *conf)
541 {
542         char *pos = buf;
543
544         memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
545         pos += sizeof(conf->hwaddr);
546
547         *(uint32_t *)pos = conf->ignore;
548         pos += 4;
549
550         if (conf->ignore)
551                 return pos - buf;
552
553         *(uint32_t *)pos = __cpu_to_be32(conf->method);
554         pos += 4;
555
556         if (conf->method == CONFIG_METHOD_STATIC) {
557                 pos += pb_protocol_serialise_string(pos,
558                                 conf->static_config.address);
559                 pos += pb_protocol_serialise_string(pos,
560                                 conf->static_config.gateway);
561                 pos += pb_protocol_serialise_string(pos,
562                                 conf->static_config.url);
563         }
564
565         *(uint32_t *)pos = conf->override;
566         pos += 4;
567
568         return pos - buf;
569 }
570
571 int pb_protocol_serialise_config(const struct config *config,
572                 char *buf, int buf_len)
573 {
574         char *pos = buf;
575         unsigned int i;
576
577         *(uint32_t *)pos = config->autoboot_enabled;
578         pos += 4;
579
580         *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
581         pos += 4;
582
583         *(uint32_t *)pos = config->safe_mode;
584         pos += 4;
585
586         *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
587         pos += 4;
588         for (i = 0; i < config->network.n_interfaces; i++) {
589                 struct interface_config *iface =
590                         config->network.interfaces[i];
591                 pos += pb_protocol_serialise_config_interface(pos, iface);
592         }
593
594         *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
595         pos += 4;
596         for (i = 0; i < config->network.n_dns_servers; i++) {
597                 pos += pb_protocol_serialise_string(pos,
598                                 config->network.dns_servers[i]);
599         }
600
601         pos += pb_protocol_serialise_string(pos, config->http_proxy);
602         pos += pb_protocol_serialise_string(pos, config->https_proxy);
603
604         *(uint32_t *)pos = __cpu_to_be32(config->n_autoboot_opts);
605         pos += 4;
606         for (i = 0; i < config->n_autoboot_opts; i++) {
607                 *(uint32_t *)pos =
608                         __cpu_to_be32(config->autoboot_opts[i].boot_type);
609                 pos += 4;
610                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
611                         *(uint32_t *)pos =
612                                 __cpu_to_be32(config->autoboot_opts[i].type);
613                         pos += 4;
614                 } else {
615                         pos += pb_protocol_serialise_string(pos,
616                                                 config->autoboot_opts[i].uuid);
617                 }
618         }
619
620         *(uint32_t *)pos = __cpu_to_be32(config->ipmi_bootdev);
621         pos += 4;
622         *(uint32_t *)pos = config->ipmi_bootdev_persistent;
623         pos += 4;
624
625         *(uint32_t *)pos = config->allow_writes;
626         pos += 4;
627
628         *(uint32_t *)pos = __cpu_to_be32(config->n_consoles);
629         pos += 4;
630         for (i = 0; i < config->n_consoles; i++)
631                 pos += pb_protocol_serialise_string(pos, config->consoles[i]);
632
633         pos += pb_protocol_serialise_string(pos, config->boot_console);
634         *(uint32_t *)pos = config->manual_console;
635         pos += 4;
636
637         pos += pb_protocol_serialise_string(pos, config->lang);
638
639         assert(pos <= buf + buf_len);
640         (void)buf_len;
641
642         return 0;
643 }
644
645 int pb_protocol_serialise_url(const char *url, char *buf, int buf_len)
646 {
647         char *pos = buf;
648
649         pos += pb_protocol_serialise_string(pos, url);
650
651         assert(pos <=buf+buf_len);
652         (void)buf_len;
653
654         return 0;
655 }
656
657 int pb_protocol_serialise_plugin_option(const struct plugin_option *opt,
658                 char *buf, int buf_len)
659 {
660         char *pos = buf;
661         unsigned int i;
662
663         pos += pb_protocol_serialise_string(pos, opt->id);
664         pos += pb_protocol_serialise_string(pos, opt->name);
665         pos += pb_protocol_serialise_string(pos, opt->vendor);
666         pos += pb_protocol_serialise_string(pos, opt->vendor_id);
667         pos += pb_protocol_serialise_string(pos, opt->version);
668         pos += pb_protocol_serialise_string(pos, opt->date);
669         pos += pb_protocol_serialise_string(pos, opt->plugin_file);
670
671         *(uint32_t *)pos = __cpu_to_be32(opt->n_executables);
672         pos += 4;
673
674         for (i = 0; i < opt->n_executables; i++)
675                 pos += pb_protocol_serialise_string(pos, opt->executables[i]);
676
677         assert(pos <= buf + buf_len);
678         (void)buf_len;
679
680         return 0;
681 }
682
683 int pb_protocol_serialise_temp_autoboot(const struct autoboot_option *opt,
684                 char *buf, int buf_len)
685 {
686         char *pos = buf;
687
688         *(uint32_t *)pos = __cpu_to_be32(opt->boot_type);
689         pos += 4;
690
691         if (opt->boot_type == BOOT_DEVICE_TYPE) {
692                 *(uint32_t *)pos = __cpu_to_be32(opt->type);
693                 pos += 4;
694         } else {
695                 pos += pb_protocol_serialise_string(pos, opt->uuid);
696         }
697
698         (void)buf_len;
699
700         return 0;
701 }
702
703 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
704 {
705         int total_len, rc;
706         char *pos;
707
708         total_len = sizeof(*message) + message->payload_len;
709
710         message->payload_len = __cpu_to_be32(message->payload_len);
711         message->action = __cpu_to_be32(message->action);
712
713         for (pos = (void *)message; total_len;) {
714                 rc = write(fd, pos, total_len);
715
716                 if (rc <= 0)
717                         break;
718
719                 total_len -= rc;
720                 pos += rc;
721         }
722
723         talloc_free(message);
724
725         if (!total_len)
726                 return 0;
727
728         pb_log_fn("failed: %s\n", strerror(errno));
729         return -1;
730 }
731
732 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
733                 enum pb_protocol_action action, int payload_len)
734 {
735         struct pb_protocol_message *message;
736
737         if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
738                 pb_log_fn("payload too big %u/%u\n", payload_len,
739                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
740                 return NULL;
741         }
742
743         message = talloc_size(ctx, sizeof(*message) + payload_len);
744
745         /* we convert these to big-endian in write_message() */
746         message->action = action;
747         message->payload_len = payload_len;
748
749         return message;
750
751 }
752
753 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
754 {
755         struct pb_protocol_message *message, m;
756         int rc;
757         unsigned int len;
758
759         /* use the stack for the initial 8-byte read */
760
761         rc = read(fd, &m, sizeof(m));
762         if (rc != sizeof(m))
763                 return NULL;
764
765         m.payload_len = __be32_to_cpu(m.payload_len);
766         m.action = __be32_to_cpu(m.action);
767
768         if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
769                 pb_log_fn("payload too big %u/%u\n", m.payload_len,
770                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
771                 return NULL;
772         }
773
774         message = talloc_size(ctx, sizeof(m) + m.payload_len);
775         memcpy(message, &m, sizeof(m));
776
777         for (len = 0; len < m.payload_len;) {
778                 rc = read(fd, message->payload + len, m.payload_len - len);
779
780                 if (rc <= 0) {
781                         talloc_free(message);
782                         pb_log_fn("failed (%u): %s\n", len,
783                                 strerror(errno));
784                         return NULL;
785                 }
786
787                 len += rc;
788         }
789
790         return message;
791 }
792
793
794 int pb_protocol_deserialise_device(struct device *dev,
795                 const struct pb_protocol_message *message)
796 {
797         unsigned int len;
798         const char *pos;
799         int rc = -1;
800
801         len = message->payload_len;
802         pos = message->payload;
803
804         if (read_string(dev, &pos, &len, &dev->id))
805                 goto out;
806
807         if (len < sizeof(enum device_type))
808                 goto out;
809         dev->type = *(enum device_type *)(pos);
810         pos += sizeof(enum device_type);
811         len -= sizeof(enum device_type);
812
813         if (read_string(dev, &pos, &len, &dev->name))
814                 goto out;
815
816         if (read_string(dev, &pos, &len, &dev->description))
817                 goto out;
818
819         if (read_string(dev, &pos, &len, &dev->icon_file))
820                 goto out;
821
822         rc = 0;
823
824 out:
825         return rc;
826 }
827
828 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
829                 const struct pb_protocol_message *message)
830 {
831         unsigned int len;
832         const char *pos;
833         int rc = -1;
834
835         len = message->payload_len;
836         pos = message->payload;
837
838         if (read_string(opt, &pos, &len, &opt->device_id))
839                 goto out;
840
841         if (read_string(opt, &pos, &len, &opt->id))
842                 goto out;
843
844         if (read_string(opt, &pos, &len, &opt->name))
845                 goto out;
846
847         if (read_string(opt, &pos, &len, &opt->description))
848                 goto out;
849
850         if (read_string(opt, &pos, &len, &opt->icon_file))
851                 goto out;
852
853         if (read_string(opt, &pos, &len, &opt->boot_image_file))
854                 goto out;
855
856         if (read_string(opt, &pos, &len, &opt->initrd_file))
857                 goto out;
858
859         if (read_string(opt, &pos, &len, &opt->dtb_file))
860                 goto out;
861
862         if (read_string(opt, &pos, &len, &opt->boot_args))
863                 goto out;
864
865         if (read_string(opt, &pos, &len, &opt->args_sig_file))
866                 goto out;
867
868         if (len < sizeof(bool))
869                 goto out;
870         opt->is_default = *(bool *)(pos);
871         pos += sizeof(bool);
872         len -= sizeof(bool);
873
874         if (read_u32(&pos, &len, &opt->type))
875                 return -1;
876
877         rc = 0;
878
879 out:
880         return rc;
881 }
882
883 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
884                 const struct pb_protocol_message *message)
885 {
886         unsigned int len;
887         const char *pos;
888         int rc = -1;
889
890         len = message->payload_len;
891         pos = message->payload;
892
893         if (read_string(cmd, &pos, &len, &cmd->option_id))
894                 goto out;
895
896         if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
897                 goto out;
898
899         if (read_string(cmd, &pos, &len, &cmd->initrd_file))
900                 goto out;
901
902         if (read_string(cmd, &pos, &len, &cmd->dtb_file))
903                 goto out;
904
905         if (read_string(cmd, &pos, &len, &cmd->boot_args))
906                 goto out;
907
908         if (read_string(cmd, &pos, &len, &cmd->args_sig_file))
909                 goto out;
910
911         if (read_string(cmd, &pos, &len, &cmd->console))
912                 goto out;
913
914         rc = 0;
915
916 out:
917         return rc;
918 }
919
920 int pb_protocol_deserialise_boot_status(struct status *status,
921                 const struct pb_protocol_message *message)
922 {
923         unsigned int len;
924         const char *pos;
925         int rc = -1;
926
927         len = message->payload_len;
928         pos = message->payload;
929
930         /* first up, the type enum... */
931         if (len < sizeof(uint32_t))
932                 goto out;
933
934         status->type = __be32_to_cpu(*(uint32_t *)(pos));
935
936         switch (status->type) {
937         case STATUS_ERROR:
938         case STATUS_INFO:
939                 break;
940         default:
941                 goto out;
942         }
943
944         pos += sizeof(uint32_t);
945         len -= sizeof(uint32_t);
946
947         /* message string */
948         if (read_string(status, &pos, &len, &status->message))
949                 goto out;
950
951         /* backlog */
952         status->backlog = *(bool *)pos;
953         pos += sizeof(status->backlog);
954
955         rc = 0;
956
957 out:
958         return rc;
959 }
960
961 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
962                 const struct pb_protocol_message *message)
963 {
964         unsigned int len, i;
965         const char *pos;
966         int rc = -1;
967         char *tmp;
968
969         len = message->payload_len;
970         pos = message->payload;
971
972         /* type and identifier strings */
973         if (read_string(sysinfo, &pos, &len, &sysinfo->type))
974                 goto out;
975
976         if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
977                 goto out;
978
979         /* Platform version strings for openpower platforms */
980         if (read_u32(&pos, &len, &sysinfo->n_primary))
981                 goto out;
982         sysinfo->platform_primary = talloc_array(sysinfo, char *,
983                                                 sysinfo->n_primary);
984         for (i = 0; i < sysinfo->n_primary; i++) {
985                 if (read_string(sysinfo, &pos, &len, &tmp))
986                         goto out;
987                 sysinfo->platform_primary[i] = talloc_strdup(sysinfo, tmp);
988         }
989
990         if (read_u32(&pos, &len, &sysinfo->n_other))
991                 goto out;
992         sysinfo->platform_other = talloc_array(sysinfo, char *,
993                                                 sysinfo->n_other);
994         for (i = 0; i < sysinfo->n_other; i++) {
995                 if (read_string(sysinfo, &pos, &len, &tmp))
996                         goto out;
997                 sysinfo->platform_other[i] = talloc_strdup(sysinfo, tmp);
998         }
999
1000         /* BMC version strings for openpower platforms */
1001         if (read_u32(&pos, &len, &sysinfo->n_bmc_current))
1002                 goto out;
1003         sysinfo->bmc_current = talloc_array(sysinfo, char *,
1004                                                 sysinfo->n_bmc_current);
1005         for (i = 0; i < sysinfo->n_bmc_current; i++) {
1006                 if (read_string(sysinfo, &pos, &len, &tmp))
1007                         goto out;
1008                 sysinfo->bmc_current[i] = talloc_strdup(sysinfo, tmp);
1009         }
1010
1011         if (read_u32(&pos, &len, &sysinfo->n_bmc_golden))
1012                 goto out;
1013         sysinfo->bmc_golden = talloc_array(sysinfo, char *,
1014                                                 sysinfo->n_bmc_golden);
1015         for (i = 0; i < sysinfo->n_bmc_golden; i++) {
1016                 if (read_string(sysinfo, &pos, &len, &tmp))
1017                         goto out;
1018                 sysinfo->bmc_golden[i] = talloc_strdup(sysinfo, tmp);
1019         }
1020
1021         /* number of interfaces */
1022         if (read_u32(&pos, &len, &sysinfo->n_interfaces))
1023                 goto out;
1024
1025         sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
1026                         sysinfo->n_interfaces);
1027
1028         for (i = 0; i < sysinfo->n_interfaces; i++) {
1029                 struct interface_info *if_info = talloc(sysinfo,
1030                                                         struct interface_info);
1031
1032                 if (read_u32(&pos, &len, &if_info->hwaddr_size))
1033                         goto out;
1034
1035                 if (len < if_info->hwaddr_size)
1036                         goto out;
1037
1038                 if_info->hwaddr = talloc_memdup(if_info, pos,
1039                                                 if_info->hwaddr_size);
1040                 pos += if_info->hwaddr_size;
1041                 len -= if_info->hwaddr_size;
1042
1043                 if (read_string(if_info, &pos, &len, &if_info->name))
1044                         goto out;
1045
1046                 if_info->link = *(bool *)pos;
1047                 pos += sizeof(if_info->link);
1048
1049                 if (read_string(if_info, &pos, &len, &if_info->address))
1050                         goto out;
1051                 if (read_string(if_info, &pos, &len, &if_info->address_v6))
1052                         goto out;
1053
1054                 sysinfo->interfaces[i] = if_info;
1055         }
1056
1057         /* number of interfaces */
1058         if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
1059                 goto out;
1060
1061         sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
1062                         sysinfo->n_blockdevs);
1063
1064         for (i = 0; i < sysinfo->n_blockdevs; i++) {
1065                 struct blockdev_info *bd_info = talloc(sysinfo,
1066                                                         struct blockdev_info);
1067
1068                 if (read_string(bd_info, &pos, &len, &bd_info->name))
1069                         goto out;
1070
1071                 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
1072                         goto out;
1073
1074                 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
1075                         goto out;
1076
1077                 sysinfo->blockdevs[i] = bd_info;
1078         }
1079
1080         for (i = 0; i < HWADDR_SIZE; i++) {
1081                 if (pos[i] != 0) {
1082                         sysinfo->bmc_mac = talloc_memdup(sysinfo, pos, HWADDR_SIZE);
1083                         break;
1084                 }
1085         }
1086
1087         pos += HWADDR_SIZE;
1088         len -= HWADDR_SIZE;
1089
1090         rc = 0;
1091 out:
1092         return rc;
1093 }
1094
1095 static int pb_protocol_deserialise_config_interface(const char **buf,
1096                 unsigned int *len, struct interface_config *iface)
1097 {
1098         unsigned int tmp;
1099
1100         if (*len < sizeof(iface->hwaddr))
1101                 return -1;
1102
1103         memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
1104         *buf += sizeof(iface->hwaddr);
1105         *len -= sizeof(iface->hwaddr);
1106
1107         if (read_u32(buf, len, &tmp))
1108                 return -1;
1109         iface->ignore = !!tmp;
1110
1111         if (iface->ignore)
1112                 return 0;
1113
1114         if (read_u32(buf, len, &iface->method))
1115                 return -1;
1116
1117         if (iface->method == CONFIG_METHOD_STATIC) {
1118                 if (read_string(iface, buf, len, &iface->static_config.address))
1119                         return -1;
1120
1121                 if (read_string(iface, buf, len, &iface->static_config.gateway))
1122                         return -1;
1123
1124                 if (read_string(iface, buf, len, &iface->static_config.url))
1125                         return -1;
1126         }
1127
1128         if (read_u32(buf, len, &tmp))
1129                 return -1;
1130         iface->override = !!tmp;
1131
1132         return 0;
1133 }
1134
1135 int pb_protocol_deserialise_config(struct config *config,
1136                 const struct pb_protocol_message *message)
1137 {
1138         unsigned int len, i, tmp;
1139         const char *pos;
1140         int rc = -1;
1141         char *str;
1142
1143         len = message->payload_len;
1144         pos = message->payload;
1145
1146         if (read_u32(&pos, &len, &tmp))
1147                 goto out;
1148         config->autoboot_enabled = !!tmp;
1149
1150         if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
1151                 goto out;
1152
1153         if (read_u32(&pos, &len, &tmp))
1154                 goto out;
1155         config->safe_mode = !!tmp;
1156
1157         if (read_u32(&pos, &len, &config->network.n_interfaces))
1158                 goto out;
1159
1160         config->network.interfaces = talloc_array(config,
1161                         struct interface_config *, config->network.n_interfaces);
1162
1163         for (i = 0; i < config->network.n_interfaces; i++) {
1164                 struct interface_config *iface = talloc_zero(
1165                                 config->network.interfaces,
1166                                 struct interface_config);
1167                 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
1168                         goto out;
1169                 config->network.interfaces[i] = iface;
1170         }
1171
1172         if (read_u32(&pos, &len, &config->network.n_dns_servers))
1173                 goto out;
1174         config->network.dns_servers = talloc_array(config, const char *,
1175                         config->network.n_dns_servers);
1176
1177         for (i = 0; i < config->network.n_dns_servers; i++) {
1178                 if (read_string(config->network.dns_servers, &pos, &len, &str))
1179                         goto out;
1180                 config->network.dns_servers[i] = str;
1181         }
1182
1183         if (read_string(config, &pos, &len, &str))
1184                 goto out;
1185         config->http_proxy = str;
1186         if (read_string(config, &pos, &len, &str))
1187                 goto out;
1188         config->https_proxy = str;
1189
1190         if (read_u32(&pos, &len, &config->n_autoboot_opts))
1191                 goto out;
1192         config->autoboot_opts = talloc_array(config, struct autoboot_option,
1193                         config->n_autoboot_opts);
1194
1195         for (i = 0; i < config->n_autoboot_opts; i++) {
1196                 if (read_u32(&pos, &len, &tmp))
1197                         goto out;
1198                 config->autoboot_opts[i].boot_type = (int)tmp;
1199                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
1200                         if (read_u32(&pos, &len, &tmp))
1201                                 goto out;
1202                         config->autoboot_opts[i].type = tmp;
1203                 } else {
1204                         if (read_string(config, &pos, &len, &str))
1205                                 goto out;
1206                         config->autoboot_opts[i].uuid = str;
1207                 }
1208         }
1209
1210         if (read_u32(&pos, &len, &config->ipmi_bootdev))
1211                 goto out;
1212         if (read_u32(&pos, &len, &tmp))
1213                 goto out;
1214         config->ipmi_bootdev_persistent = !!tmp;
1215
1216         if (read_u32(&pos, &len, &tmp))
1217                 goto out;
1218         config->allow_writes = !!tmp;
1219
1220         if (read_u32(&pos, &len, &config->n_consoles))
1221                 goto out;
1222
1223         config->consoles = talloc_array(config, char *, config->n_consoles);
1224         for (i = 0; i < config->n_consoles; i++) {
1225                 if (read_string(config->consoles, &pos, &len, &str))
1226                         goto out;
1227                 config->consoles[i] = str;
1228         }
1229
1230         if (read_string(config, &pos, &len, &str))
1231                 goto out;
1232
1233         config->boot_console = str;
1234
1235         if (read_u32(&pos, &len, &tmp))
1236                 goto out;
1237         config->manual_console = !!tmp;
1238
1239         if (read_string(config, &pos, &len, &str))
1240                 goto out;
1241
1242         config->lang = str;
1243
1244         rc = 0;
1245
1246 out:
1247         return rc;
1248 }
1249
1250 int pb_protocol_deserialise_plugin_option(struct plugin_option *opt,
1251                 const struct pb_protocol_message *message)
1252 {
1253         unsigned int len, i, tmp;
1254         const char *pos;
1255         int rc = -1;
1256         char *str;
1257
1258         len = message->payload_len;
1259         pos = message->payload;
1260
1261         if (read_string(opt, &pos, &len, &str))
1262                 goto out;
1263         opt->id = str;
1264
1265         if (read_string(opt, &pos, &len, &str))
1266                 goto out;
1267         opt->name = str;
1268
1269         if (read_string(opt, &pos, &len, &str))
1270                 goto out;
1271         opt->vendor = str;
1272
1273         if (read_string(opt, &pos, &len, &str))
1274                 goto out;
1275         opt->vendor_id = str;
1276
1277         if (read_string(opt, &pos, &len, &str))
1278                 goto out;
1279         opt->version = str;
1280
1281         if (read_string(opt, &pos, &len, &str))
1282                 goto out;
1283         opt->date = str;
1284
1285         if (read_string(opt, &pos, &len, &str))
1286                 goto out;
1287         opt->plugin_file = str;
1288
1289         if (read_u32(&pos, &len, &tmp))
1290                 goto out;
1291         opt->n_executables = tmp;
1292
1293         opt->executables = talloc_zero_array(opt, char *, opt->n_executables);
1294         if (!opt->executables)
1295                 goto out;
1296
1297         for (i = 0; i < opt->n_executables; i++) {
1298                 if (read_string(opt, &pos, &len, &str))
1299                         goto out;
1300                 opt->executables[i] = talloc_strdup(opt, str);
1301         }
1302
1303         rc = 0;
1304 out:
1305         return rc;
1306 }
1307
1308 int pb_protocol_deserialise_temp_autoboot(struct autoboot_option *opt,
1309                 const struct pb_protocol_message *message)
1310 {
1311         unsigned int len, tmp;
1312         const char *pos;
1313         int rc = -1;
1314         char *str;
1315
1316         len = message->payload_len;
1317         pos = message->payload;
1318
1319         if (read_u32(&pos, &len, &tmp))
1320                 goto out;
1321
1322         opt->boot_type = tmp;
1323         if (opt->boot_type == BOOT_DEVICE_TYPE) {
1324                 if (read_u32(&pos, &len, &tmp))
1325                         goto out;
1326                 opt->type = tmp;
1327
1328         } else if (opt->boot_type == BOOT_DEVICE_UUID) {
1329                 if (read_string(opt, &pos, &len, &str))
1330                         goto out;
1331                 opt->uuid = str;
1332
1333         } else {
1334                 return -1;
1335         }
1336
1337         rc = 0;
1338
1339 out:
1340         return rc;
1341 }