]> git.ozlabs.org Git - petitboot/blob - lib/pb-protocol/pb-protocol.c
lib/crypt: Add helpers for operating on /etc/shadow
[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;      /* boot_active */
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         *(bool *)pos = __cpu_to_be32(status->boot_active);
461         pos += sizeof(bool);
462
463         assert(pos <= buf + buf_len);
464         (void)buf_len;
465
466         return 0;
467 }
468
469 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
470                 char *buf, int buf_len)
471 {
472         char *pos = buf;
473         unsigned int i;
474
475         pos += pb_protocol_serialise_string(pos, sysinfo->type);
476         pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
477
478         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_primary);
479         pos += sizeof(uint32_t);
480         for (i = 0; i < sysinfo->n_primary; i++)
481                 pos += pb_protocol_serialise_string(pos, sysinfo->platform_primary[i]);
482
483         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_other);
484         pos += sizeof(uint32_t);
485         for (i = 0; i < sysinfo->n_other; i++)
486                 pos += pb_protocol_serialise_string(pos, sysinfo->platform_other[i]);
487
488         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_current);
489         pos += sizeof(uint32_t);
490         for (i = 0; i < sysinfo->n_bmc_current; i++)
491                 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_current[i]);
492
493         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_golden);
494         pos += sizeof(uint32_t);
495         for (i = 0; i < sysinfo->n_bmc_golden; i++)
496                 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_golden[i]);
497
498         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
499         pos += sizeof(uint32_t);
500
501         for (i = 0; i < sysinfo->n_interfaces; i++) {
502                 struct interface_info *if_info = sysinfo->interfaces[i];
503
504                 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
505                 pos += sizeof(uint32_t);
506
507                 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
508                 pos += if_info->hwaddr_size;
509
510                 pos += pb_protocol_serialise_string(pos, if_info->name);
511
512                 *(bool *)pos = if_info->link;
513                 pos += sizeof(bool);
514
515                 pos += pb_protocol_serialise_string(pos, if_info->address);
516                 pos += pb_protocol_serialise_string(pos, if_info->address_v6);
517         }
518
519         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
520         pos += sizeof(uint32_t);
521
522         for (i = 0; i < sysinfo->n_blockdevs; i++) {
523                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
524
525                 pos += pb_protocol_serialise_string(pos, bd_info->name);
526                 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
527                 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
528         }
529
530         if (sysinfo->bmc_mac)
531                 memcpy(pos, sysinfo->bmc_mac, HWADDR_SIZE);
532         else
533                 memset(pos, 0, HWADDR_SIZE);
534         pos += HWADDR_SIZE;
535
536         assert(pos <= buf + buf_len);
537         (void)buf_len;
538
539         return 0;
540 }
541
542 static int pb_protocol_serialise_config_interface(char *buf,
543                 struct interface_config *conf)
544 {
545         char *pos = buf;
546
547         memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
548         pos += sizeof(conf->hwaddr);
549
550         *(uint32_t *)pos = conf->ignore;
551         pos += 4;
552
553         if (conf->ignore)
554                 return pos - buf;
555
556         *(uint32_t *)pos = __cpu_to_be32(conf->method);
557         pos += 4;
558
559         if (conf->method == CONFIG_METHOD_STATIC) {
560                 pos += pb_protocol_serialise_string(pos,
561                                 conf->static_config.address);
562                 pos += pb_protocol_serialise_string(pos,
563                                 conf->static_config.gateway);
564                 pos += pb_protocol_serialise_string(pos,
565                                 conf->static_config.url);
566         }
567
568         *(uint32_t *)pos = conf->override;
569         pos += 4;
570
571         return pos - buf;
572 }
573
574 int pb_protocol_serialise_config(const struct config *config,
575                 char *buf, int buf_len)
576 {
577         char *pos = buf;
578         unsigned int i;
579
580         *(uint32_t *)pos = config->autoboot_enabled;
581         pos += 4;
582
583         *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
584         pos += 4;
585
586         *(uint32_t *)pos = config->safe_mode;
587         pos += 4;
588
589         *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
590         pos += 4;
591         for (i = 0; i < config->network.n_interfaces; i++) {
592                 struct interface_config *iface =
593                         config->network.interfaces[i];
594                 pos += pb_protocol_serialise_config_interface(pos, iface);
595         }
596
597         *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
598         pos += 4;
599         for (i = 0; i < config->network.n_dns_servers; i++) {
600                 pos += pb_protocol_serialise_string(pos,
601                                 config->network.dns_servers[i]);
602         }
603
604         pos += pb_protocol_serialise_string(pos, config->http_proxy);
605         pos += pb_protocol_serialise_string(pos, config->https_proxy);
606
607         *(uint32_t *)pos = __cpu_to_be32(config->n_autoboot_opts);
608         pos += 4;
609         for (i = 0; i < config->n_autoboot_opts; i++) {
610                 *(uint32_t *)pos =
611                         __cpu_to_be32(config->autoboot_opts[i].boot_type);
612                 pos += 4;
613                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
614                         *(uint32_t *)pos =
615                                 __cpu_to_be32(config->autoboot_opts[i].type);
616                         pos += 4;
617                 } else {
618                         pos += pb_protocol_serialise_string(pos,
619                                                 config->autoboot_opts[i].uuid);
620                 }
621         }
622
623         *(uint32_t *)pos = __cpu_to_be32(config->ipmi_bootdev);
624         pos += 4;
625         *(uint32_t *)pos = config->ipmi_bootdev_persistent;
626         pos += 4;
627
628         *(uint32_t *)pos = config->allow_writes;
629         pos += 4;
630
631         *(uint32_t *)pos = __cpu_to_be32(config->n_consoles);
632         pos += 4;
633         for (i = 0; i < config->n_consoles; i++)
634                 pos += pb_protocol_serialise_string(pos, config->consoles[i]);
635
636         pos += pb_protocol_serialise_string(pos, config->boot_console);
637         *(uint32_t *)pos = config->manual_console;
638         pos += 4;
639
640         pos += pb_protocol_serialise_string(pos, config->lang);
641
642         assert(pos <= buf + buf_len);
643         (void)buf_len;
644
645         return 0;
646 }
647
648 int pb_protocol_serialise_url(const char *url, char *buf, int buf_len)
649 {
650         char *pos = buf;
651
652         pos += pb_protocol_serialise_string(pos, url);
653
654         assert(pos <=buf+buf_len);
655         (void)buf_len;
656
657         return 0;
658 }
659
660 int pb_protocol_serialise_plugin_option(const struct plugin_option *opt,
661                 char *buf, int buf_len)
662 {
663         char *pos = buf;
664         unsigned int i;
665
666         pos += pb_protocol_serialise_string(pos, opt->id);
667         pos += pb_protocol_serialise_string(pos, opt->name);
668         pos += pb_protocol_serialise_string(pos, opt->vendor);
669         pos += pb_protocol_serialise_string(pos, opt->vendor_id);
670         pos += pb_protocol_serialise_string(pos, opt->version);
671         pos += pb_protocol_serialise_string(pos, opt->date);
672         pos += pb_protocol_serialise_string(pos, opt->plugin_file);
673
674         *(uint32_t *)pos = __cpu_to_be32(opt->n_executables);
675         pos += 4;
676
677         for (i = 0; i < opt->n_executables; i++)
678                 pos += pb_protocol_serialise_string(pos, opt->executables[i]);
679
680         assert(pos <= buf + buf_len);
681         (void)buf_len;
682
683         return 0;
684 }
685
686 int pb_protocol_serialise_temp_autoboot(const struct autoboot_option *opt,
687                 char *buf, int buf_len)
688 {
689         char *pos = buf;
690
691         *(uint32_t *)pos = __cpu_to_be32(opt->boot_type);
692         pos += 4;
693
694         if (opt->boot_type == BOOT_DEVICE_TYPE) {
695                 *(uint32_t *)pos = __cpu_to_be32(opt->type);
696                 pos += 4;
697         } else {
698                 pos += pb_protocol_serialise_string(pos, opt->uuid);
699         }
700
701         (void)buf_len;
702
703         return 0;
704 }
705
706 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
707 {
708         int total_len, rc;
709         char *pos;
710
711         total_len = sizeof(*message) + message->payload_len;
712
713         message->payload_len = __cpu_to_be32(message->payload_len);
714         message->action = __cpu_to_be32(message->action);
715
716         for (pos = (void *)message; total_len;) {
717                 rc = write(fd, pos, total_len);
718
719                 if (rc <= 0)
720                         break;
721
722                 total_len -= rc;
723                 pos += rc;
724         }
725
726         talloc_free(message);
727
728         if (!total_len)
729                 return 0;
730
731         pb_log_fn("failed: %s\n", strerror(errno));
732         return -1;
733 }
734
735 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
736                 enum pb_protocol_action action, int payload_len)
737 {
738         struct pb_protocol_message *message;
739
740         if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
741                 pb_log_fn("payload too big %u/%u\n", payload_len,
742                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
743                 return NULL;
744         }
745
746         message = talloc_size(ctx, sizeof(*message) + payload_len);
747
748         /* we convert these to big-endian in write_message() */
749         message->action = action;
750         message->payload_len = payload_len;
751
752         return message;
753
754 }
755
756 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
757 {
758         struct pb_protocol_message *message, m;
759         int rc;
760         unsigned int len;
761
762         /* use the stack for the initial 8-byte read */
763
764         rc = read(fd, &m, sizeof(m));
765         if (rc != sizeof(m))
766                 return NULL;
767
768         m.payload_len = __be32_to_cpu(m.payload_len);
769         m.action = __be32_to_cpu(m.action);
770
771         if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
772                 pb_log_fn("payload too big %u/%u\n", m.payload_len,
773                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
774                 return NULL;
775         }
776
777         message = talloc_size(ctx, sizeof(m) + m.payload_len);
778         memcpy(message, &m, sizeof(m));
779
780         for (len = 0; len < m.payload_len;) {
781                 rc = read(fd, message->payload + len, m.payload_len - len);
782
783                 if (rc <= 0) {
784                         talloc_free(message);
785                         pb_log_fn("failed (%u): %s\n", len,
786                                 strerror(errno));
787                         return NULL;
788                 }
789
790                 len += rc;
791         }
792
793         return message;
794 }
795
796
797 int pb_protocol_deserialise_device(struct device *dev,
798                 const struct pb_protocol_message *message)
799 {
800         unsigned int len;
801         const char *pos;
802         int rc = -1;
803
804         len = message->payload_len;
805         pos = message->payload;
806
807         if (read_string(dev, &pos, &len, &dev->id))
808                 goto out;
809
810         if (len < sizeof(enum device_type))
811                 goto out;
812         dev->type = *(enum device_type *)(pos);
813         pos += sizeof(enum device_type);
814         len -= sizeof(enum device_type);
815
816         if (read_string(dev, &pos, &len, &dev->name))
817                 goto out;
818
819         if (read_string(dev, &pos, &len, &dev->description))
820                 goto out;
821
822         if (read_string(dev, &pos, &len, &dev->icon_file))
823                 goto out;
824
825         rc = 0;
826
827 out:
828         return rc;
829 }
830
831 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
832                 const struct pb_protocol_message *message)
833 {
834         unsigned int len;
835         const char *pos;
836         int rc = -1;
837
838         len = message->payload_len;
839         pos = message->payload;
840
841         if (read_string(opt, &pos, &len, &opt->device_id))
842                 goto out;
843
844         if (read_string(opt, &pos, &len, &opt->id))
845                 goto out;
846
847         if (read_string(opt, &pos, &len, &opt->name))
848                 goto out;
849
850         if (read_string(opt, &pos, &len, &opt->description))
851                 goto out;
852
853         if (read_string(opt, &pos, &len, &opt->icon_file))
854                 goto out;
855
856         if (read_string(opt, &pos, &len, &opt->boot_image_file))
857                 goto out;
858
859         if (read_string(opt, &pos, &len, &opt->initrd_file))
860                 goto out;
861
862         if (read_string(opt, &pos, &len, &opt->dtb_file))
863                 goto out;
864
865         if (read_string(opt, &pos, &len, &opt->boot_args))
866                 goto out;
867
868         if (read_string(opt, &pos, &len, &opt->args_sig_file))
869                 goto out;
870
871         if (len < sizeof(bool))
872                 goto out;
873         opt->is_default = *(bool *)(pos);
874         pos += sizeof(bool);
875         len -= sizeof(bool);
876
877         if (read_u32(&pos, &len, &opt->type))
878                 return -1;
879
880         rc = 0;
881
882 out:
883         return rc;
884 }
885
886 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
887                 const struct pb_protocol_message *message)
888 {
889         unsigned int len;
890         const char *pos;
891         int rc = -1;
892
893         len = message->payload_len;
894         pos = message->payload;
895
896         if (read_string(cmd, &pos, &len, &cmd->option_id))
897                 goto out;
898
899         if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
900                 goto out;
901
902         if (read_string(cmd, &pos, &len, &cmd->initrd_file))
903                 goto out;
904
905         if (read_string(cmd, &pos, &len, &cmd->dtb_file))
906                 goto out;
907
908         if (read_string(cmd, &pos, &len, &cmd->boot_args))
909                 goto out;
910
911         if (read_string(cmd, &pos, &len, &cmd->args_sig_file))
912                 goto out;
913
914         if (read_string(cmd, &pos, &len, &cmd->console))
915                 goto out;
916
917         rc = 0;
918
919 out:
920         return rc;
921 }
922
923 int pb_protocol_deserialise_boot_status(struct status *status,
924                 const struct pb_protocol_message *message)
925 {
926         unsigned int len;
927         const char *pos;
928         int rc = -1;
929
930         len = message->payload_len;
931         pos = message->payload;
932
933         /* first up, the type enum... */
934         if (len < sizeof(uint32_t))
935                 goto out;
936
937         status->type = __be32_to_cpu(*(uint32_t *)(pos));
938
939         switch (status->type) {
940         case STATUS_ERROR:
941         case STATUS_INFO:
942                 break;
943         default:
944                 goto out;
945         }
946
947         pos += sizeof(uint32_t);
948         len -= sizeof(uint32_t);
949
950         /* message string */
951         if (read_string(status, &pos, &len, &status->message))
952                 goto out;
953
954         /* backlog */
955         status->backlog = *(bool *)pos;
956         pos += sizeof(status->backlog);
957
958         /* boot_active */
959         status->boot_active = *(bool *)pos;
960         pos += sizeof(status->boot_active);
961
962         rc = 0;
963
964 out:
965         return rc;
966 }
967
968 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
969                 const struct pb_protocol_message *message)
970 {
971         unsigned int len, i;
972         const char *pos;
973         int rc = -1;
974         char *tmp;
975
976         len = message->payload_len;
977         pos = message->payload;
978
979         /* type and identifier strings */
980         if (read_string(sysinfo, &pos, &len, &sysinfo->type))
981                 goto out;
982
983         if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
984                 goto out;
985
986         /* Platform version strings for openpower platforms */
987         if (read_u32(&pos, &len, &sysinfo->n_primary))
988                 goto out;
989         sysinfo->platform_primary = talloc_array(sysinfo, char *,
990                                                 sysinfo->n_primary);
991         for (i = 0; i < sysinfo->n_primary; i++) {
992                 if (read_string(sysinfo, &pos, &len, &tmp))
993                         goto out;
994                 sysinfo->platform_primary[i] = talloc_strdup(sysinfo, tmp);
995         }
996
997         if (read_u32(&pos, &len, &sysinfo->n_other))
998                 goto out;
999         sysinfo->platform_other = talloc_array(sysinfo, char *,
1000                                                 sysinfo->n_other);
1001         for (i = 0; i < sysinfo->n_other; i++) {
1002                 if (read_string(sysinfo, &pos, &len, &tmp))
1003                         goto out;
1004                 sysinfo->platform_other[i] = talloc_strdup(sysinfo, tmp);
1005         }
1006
1007         /* BMC version strings for openpower platforms */
1008         if (read_u32(&pos, &len, &sysinfo->n_bmc_current))
1009                 goto out;
1010         sysinfo->bmc_current = talloc_array(sysinfo, char *,
1011                                                 sysinfo->n_bmc_current);
1012         for (i = 0; i < sysinfo->n_bmc_current; i++) {
1013                 if (read_string(sysinfo, &pos, &len, &tmp))
1014                         goto out;
1015                 sysinfo->bmc_current[i] = talloc_strdup(sysinfo, tmp);
1016         }
1017
1018         if (read_u32(&pos, &len, &sysinfo->n_bmc_golden))
1019                 goto out;
1020         sysinfo->bmc_golden = talloc_array(sysinfo, char *,
1021                                                 sysinfo->n_bmc_golden);
1022         for (i = 0; i < sysinfo->n_bmc_golden; i++) {
1023                 if (read_string(sysinfo, &pos, &len, &tmp))
1024                         goto out;
1025                 sysinfo->bmc_golden[i] = talloc_strdup(sysinfo, tmp);
1026         }
1027
1028         /* number of interfaces */
1029         if (read_u32(&pos, &len, &sysinfo->n_interfaces))
1030                 goto out;
1031
1032         sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
1033                         sysinfo->n_interfaces);
1034
1035         for (i = 0; i < sysinfo->n_interfaces; i++) {
1036                 struct interface_info *if_info = talloc(sysinfo,
1037                                                         struct interface_info);
1038
1039                 if (read_u32(&pos, &len, &if_info->hwaddr_size))
1040                         goto out;
1041
1042                 if (len < if_info->hwaddr_size)
1043                         goto out;
1044
1045                 if_info->hwaddr = talloc_memdup(if_info, pos,
1046                                                 if_info->hwaddr_size);
1047                 pos += if_info->hwaddr_size;
1048                 len -= if_info->hwaddr_size;
1049
1050                 if (read_string(if_info, &pos, &len, &if_info->name))
1051                         goto out;
1052
1053                 if_info->link = *(bool *)pos;
1054                 pos += sizeof(if_info->link);
1055
1056                 if (read_string(if_info, &pos, &len, &if_info->address))
1057                         goto out;
1058                 if (read_string(if_info, &pos, &len, &if_info->address_v6))
1059                         goto out;
1060
1061                 sysinfo->interfaces[i] = if_info;
1062         }
1063
1064         /* number of interfaces */
1065         if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
1066                 goto out;
1067
1068         sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
1069                         sysinfo->n_blockdevs);
1070
1071         for (i = 0; i < sysinfo->n_blockdevs; i++) {
1072                 struct blockdev_info *bd_info = talloc(sysinfo,
1073                                                         struct blockdev_info);
1074
1075                 if (read_string(bd_info, &pos, &len, &bd_info->name))
1076                         goto out;
1077
1078                 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
1079                         goto out;
1080
1081                 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
1082                         goto out;
1083
1084                 sysinfo->blockdevs[i] = bd_info;
1085         }
1086
1087         for (i = 0; i < HWADDR_SIZE; i++) {
1088                 if (pos[i] != 0) {
1089                         sysinfo->bmc_mac = talloc_memdup(sysinfo, pos, HWADDR_SIZE);
1090                         break;
1091                 }
1092         }
1093
1094         pos += HWADDR_SIZE;
1095         len -= HWADDR_SIZE;
1096
1097         rc = 0;
1098 out:
1099         return rc;
1100 }
1101
1102 static int pb_protocol_deserialise_config_interface(const char **buf,
1103                 unsigned int *len, struct interface_config *iface)
1104 {
1105         unsigned int tmp;
1106
1107         if (*len < sizeof(iface->hwaddr))
1108                 return -1;
1109
1110         memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
1111         *buf += sizeof(iface->hwaddr);
1112         *len -= sizeof(iface->hwaddr);
1113
1114         if (read_u32(buf, len, &tmp))
1115                 return -1;
1116         iface->ignore = !!tmp;
1117
1118         if (iface->ignore)
1119                 return 0;
1120
1121         if (read_u32(buf, len, &iface->method))
1122                 return -1;
1123
1124         if (iface->method == CONFIG_METHOD_STATIC) {
1125                 if (read_string(iface, buf, len, &iface->static_config.address))
1126                         return -1;
1127
1128                 if (read_string(iface, buf, len, &iface->static_config.gateway))
1129                         return -1;
1130
1131                 if (read_string(iface, buf, len, &iface->static_config.url))
1132                         return -1;
1133         }
1134
1135         if (read_u32(buf, len, &tmp))
1136                 return -1;
1137         iface->override = !!tmp;
1138
1139         return 0;
1140 }
1141
1142 int pb_protocol_deserialise_config(struct config *config,
1143                 const struct pb_protocol_message *message)
1144 {
1145         unsigned int len, i, tmp;
1146         const char *pos;
1147         int rc = -1;
1148         char *str;
1149
1150         len = message->payload_len;
1151         pos = message->payload;
1152
1153         if (read_u32(&pos, &len, &tmp))
1154                 goto out;
1155         config->autoboot_enabled = !!tmp;
1156
1157         if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
1158                 goto out;
1159
1160         if (read_u32(&pos, &len, &tmp))
1161                 goto out;
1162         config->safe_mode = !!tmp;
1163
1164         if (read_u32(&pos, &len, &config->network.n_interfaces))
1165                 goto out;
1166
1167         config->network.interfaces = talloc_array(config,
1168                         struct interface_config *, config->network.n_interfaces);
1169
1170         for (i = 0; i < config->network.n_interfaces; i++) {
1171                 struct interface_config *iface = talloc_zero(
1172                                 config->network.interfaces,
1173                                 struct interface_config);
1174                 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
1175                         goto out;
1176                 config->network.interfaces[i] = iface;
1177         }
1178
1179         if (read_u32(&pos, &len, &config->network.n_dns_servers))
1180                 goto out;
1181         config->network.dns_servers = talloc_array(config, const char *,
1182                         config->network.n_dns_servers);
1183
1184         for (i = 0; i < config->network.n_dns_servers; i++) {
1185                 if (read_string(config->network.dns_servers, &pos, &len, &str))
1186                         goto out;
1187                 config->network.dns_servers[i] = str;
1188         }
1189
1190         if (read_string(config, &pos, &len, &str))
1191                 goto out;
1192         config->http_proxy = str;
1193         if (read_string(config, &pos, &len, &str))
1194                 goto out;
1195         config->https_proxy = str;
1196
1197         if (read_u32(&pos, &len, &config->n_autoboot_opts))
1198                 goto out;
1199         config->autoboot_opts = talloc_array(config, struct autoboot_option,
1200                         config->n_autoboot_opts);
1201
1202         for (i = 0; i < config->n_autoboot_opts; i++) {
1203                 if (read_u32(&pos, &len, &tmp))
1204                         goto out;
1205                 config->autoboot_opts[i].boot_type = (int)tmp;
1206                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
1207                         if (read_u32(&pos, &len, &tmp))
1208                                 goto out;
1209                         config->autoboot_opts[i].type = tmp;
1210                 } else {
1211                         if (read_string(config, &pos, &len, &str))
1212                                 goto out;
1213                         config->autoboot_opts[i].uuid = str;
1214                 }
1215         }
1216
1217         if (read_u32(&pos, &len, &config->ipmi_bootdev))
1218                 goto out;
1219         if (read_u32(&pos, &len, &tmp))
1220                 goto out;
1221         config->ipmi_bootdev_persistent = !!tmp;
1222
1223         if (read_u32(&pos, &len, &tmp))
1224                 goto out;
1225         config->allow_writes = !!tmp;
1226
1227         if (read_u32(&pos, &len, &config->n_consoles))
1228                 goto out;
1229
1230         config->consoles = talloc_array(config, char *, config->n_consoles);
1231         for (i = 0; i < config->n_consoles; i++) {
1232                 if (read_string(config->consoles, &pos, &len, &str))
1233                         goto out;
1234                 config->consoles[i] = str;
1235         }
1236
1237         if (read_string(config, &pos, &len, &str))
1238                 goto out;
1239
1240         config->boot_console = str;
1241
1242         if (read_u32(&pos, &len, &tmp))
1243                 goto out;
1244         config->manual_console = !!tmp;
1245
1246         if (read_string(config, &pos, &len, &str))
1247                 goto out;
1248
1249         config->lang = str;
1250
1251         rc = 0;
1252
1253 out:
1254         return rc;
1255 }
1256
1257 int pb_protocol_deserialise_plugin_option(struct plugin_option *opt,
1258                 const struct pb_protocol_message *message)
1259 {
1260         unsigned int len, i, tmp;
1261         const char *pos;
1262         int rc = -1;
1263         char *str;
1264
1265         len = message->payload_len;
1266         pos = message->payload;
1267
1268         if (read_string(opt, &pos, &len, &str))
1269                 goto out;
1270         opt->id = str;
1271
1272         if (read_string(opt, &pos, &len, &str))
1273                 goto out;
1274         opt->name = str;
1275
1276         if (read_string(opt, &pos, &len, &str))
1277                 goto out;
1278         opt->vendor = str;
1279
1280         if (read_string(opt, &pos, &len, &str))
1281                 goto out;
1282         opt->vendor_id = str;
1283
1284         if (read_string(opt, &pos, &len, &str))
1285                 goto out;
1286         opt->version = str;
1287
1288         if (read_string(opt, &pos, &len, &str))
1289                 goto out;
1290         opt->date = str;
1291
1292         if (read_string(opt, &pos, &len, &str))
1293                 goto out;
1294         opt->plugin_file = str;
1295
1296         if (read_u32(&pos, &len, &tmp))
1297                 goto out;
1298         opt->n_executables = tmp;
1299
1300         opt->executables = talloc_zero_array(opt, char *, opt->n_executables);
1301         if (!opt->executables)
1302                 goto out;
1303
1304         for (i = 0; i < opt->n_executables; i++) {
1305                 if (read_string(opt, &pos, &len, &str))
1306                         goto out;
1307                 opt->executables[i] = talloc_strdup(opt, str);
1308         }
1309
1310         rc = 0;
1311 out:
1312         return rc;
1313 }
1314
1315 int pb_protocol_deserialise_temp_autoboot(struct autoboot_option *opt,
1316                 const struct pb_protocol_message *message)
1317 {
1318         unsigned int len, tmp;
1319         const char *pos;
1320         int rc = -1;
1321         char *str;
1322
1323         len = message->payload_len;
1324         pos = message->payload;
1325
1326         if (read_u32(&pos, &len, &tmp))
1327                 goto out;
1328
1329         opt->boot_type = tmp;
1330         if (opt->boot_type == BOOT_DEVICE_TYPE) {
1331                 if (read_u32(&pos, &len, &tmp))
1332                         goto out;
1333                 opt->type = tmp;
1334
1335         } else if (opt->boot_type == BOOT_DEVICE_UUID) {
1336                 if (read_string(opt, &pos, &len, &str))
1337                         goto out;
1338                 opt->uuid = str;
1339
1340         } else {
1341                 return -1;
1342         }
1343
1344         rc = 0;
1345
1346 out:
1347         return rc;
1348 }