]> git.ozlabs.org Git - petitboot/blob - lib/pb-protocol/pb-protocol.c
Support HTTP(S) proxies when downloading resources
[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         }
79 }
80
81 int pb_protocol_device_cmp(const struct device *a, const struct device *b)
82 {
83         return !strcmp(a->id, b->id);
84 }
85
86 int pb_protocol_boot_option_cmp(const struct boot_option *a,
87         const struct boot_option *b)
88 {
89         return !strcmp(a->id, b->id);
90 }
91
92 /* Write a string into the buffer, starting at pos.
93  *
94  * Returns the total length used for the write, including length header.
95  */
96 int pb_protocol_serialise_string(char *pos, const char *str)
97 {
98         int len = 0;
99
100         if (str)
101                 len = strlen(str);
102
103         *(uint32_t *)pos = __cpu_to_be32(len);
104         pos += sizeof(uint32_t);
105
106         memcpy(pos, str, len);
107
108         return len + sizeof(uint32_t);
109 }
110
111 /* Read a string from a buffer, allocating the new string as necessary.
112  *
113  * @param[in] ctx       The talloc context to base the allocation on
114  * @param[in,out] pos   Where to start reading
115  * @param[in,out] len   The amount of data remaining in the buffer
116  * @param[out] str      Pointer to resuling string
117  * @return              zero on success, non-zero on failure
118  */
119 static int read_string(void *ctx, const char **pos, unsigned int *len,
120         char **str)
121 {
122         uint32_t str_len, read_len;
123
124         if (*len < sizeof(uint32_t))
125                 return -1;
126
127         str_len = __be32_to_cpu(*(uint32_t *)(*pos));
128         read_len = sizeof(uint32_t);
129
130         if (read_len + str_len > *len)
131                 return -1;
132
133         if (str_len == 0)
134                 *str = NULL;
135         else
136                 *str = talloc_strndup(ctx, *pos + read_len, str_len);
137
138         read_len += str_len;
139
140         /* all ok, update the caller's pointers */
141         *pos += read_len;
142         *len -= read_len;
143
144         return 0;
145 }
146
147 static int read_u32(const char **pos, unsigned int *len, unsigned int *p)
148 {
149         if (*len < sizeof(uint32_t))
150                 return -1;
151
152         *p = (unsigned int)__be32_to_cpu(*(uint32_t *)(*pos));
153         *pos += sizeof(uint32_t);
154         *len -= sizeof(uint32_t);
155
156         return 0;
157 }
158
159 char *pb_protocol_deserialise_string(void *ctx,
160                 const struct pb_protocol_message *message)
161 {
162         const char *buf;
163         char *str;
164         unsigned int len;
165
166         len = message->payload_len;
167         buf = message->payload;
168
169         if (read_string(ctx, &buf, &len, &str))
170                 return NULL;
171
172         return str;
173 }
174
175 static int optional_strlen(const char *str)
176 {
177         if (!str)
178                 return 0;
179         return strlen(str);
180 }
181
182 int pb_protocol_device_len(const struct device *dev)
183 {
184         return  4 + optional_strlen(dev->id) +
185                 sizeof(dev->type) +
186                 4 + optional_strlen(dev->name) +
187                 4 + optional_strlen(dev->description) +
188                 4 + optional_strlen(dev->icon_file);
189 }
190
191 int pb_protocol_boot_option_len(const struct boot_option *opt)
192 {
193
194         return  4 + optional_strlen(opt->device_id) +
195                 4 + optional_strlen(opt->id) +
196                 4 + optional_strlen(opt->name) +
197                 4 + optional_strlen(opt->description) +
198                 4 + optional_strlen(opt->icon_file) +
199                 4 + optional_strlen(opt->boot_image_file) +
200                 4 + optional_strlen(opt->initrd_file) +
201                 4 + optional_strlen(opt->dtb_file) +
202                 4 + optional_strlen(opt->boot_args) +
203                 4 + optional_strlen(opt->args_sig_file) +
204                 sizeof(opt->is_default);
205 }
206
207 int pb_protocol_boot_len(const struct boot_command *boot)
208 {
209         return  4 + optional_strlen(boot->option_id) +
210                 4 + optional_strlen(boot->boot_image_file) +
211                 4 + optional_strlen(boot->initrd_file) +
212                 4 + optional_strlen(boot->dtb_file) +
213                 4 + optional_strlen(boot->boot_args) +
214                 4 + optional_strlen(boot->args_sig_file) +
215                 4 + optional_strlen(boot->console);
216 }
217
218 int pb_protocol_boot_status_len(const struct boot_status *status)
219 {
220         return  4 +
221                 4 + optional_strlen(status->message) +
222                 4 + optional_strlen(status->detail) +
223                 4;
224 }
225
226 int pb_protocol_system_info_len(const struct system_info *sysinfo)
227 {
228         unsigned int len, i;
229
230         len =   4 + optional_strlen(sysinfo->type) +
231                 4 + optional_strlen(sysinfo->identifier) +
232                 4 + 4;
233
234         len +=  4;
235         for (i = 0; i < sysinfo->n_primary; i++)
236                 len += 4 + optional_strlen(sysinfo->platform_primary[i]);
237         len +=  4;
238         for (i = 0; i < sysinfo->n_other; i++)
239                 len += 4 + optional_strlen(sysinfo->platform_other[i]);
240
241         len +=  4;
242         for (i = 0; i < sysinfo->n_bmc_current; i++)
243                 len += 4 + optional_strlen(sysinfo->bmc_current[i]);
244         len +=  4;
245         for (i = 0; i < sysinfo->n_bmc_golden; i++)
246                 len += 4 + optional_strlen(sysinfo->bmc_golden[i]);
247
248         for (i = 0; i < sysinfo->n_interfaces; i++) {
249                 struct interface_info *if_info = sysinfo->interfaces[i];
250                 len +=  4 + if_info->hwaddr_size +
251                         4 + optional_strlen(if_info->name) +
252                         sizeof(if_info->link);
253         }
254
255         for (i = 0; i < sysinfo->n_blockdevs; i++) {
256                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
257                 len +=  4 + optional_strlen(bd_info->name) +
258                         4 + optional_strlen(bd_info->uuid) +
259                         4 + optional_strlen(bd_info->mountpoint);
260         }
261
262         /* BMC MAC */
263         len += HWADDR_SIZE;
264
265         return len;
266 }
267
268 static int pb_protocol_interface_config_len(struct interface_config *conf)
269 {
270         unsigned int len;
271
272         len =   sizeof(conf->hwaddr) +
273                 4 /* conf->ignore */;
274
275         if (conf->ignore)
276                 return len;
277
278         len += 4 /* conf->method */;
279
280         if (conf->method == CONFIG_METHOD_STATIC) {
281                 len += 4 + optional_strlen(conf->static_config.address);
282                 len += 4 + optional_strlen(conf->static_config.gateway);
283                 len += 4 + optional_strlen(conf->static_config.url);
284         }
285
286         len += 4 /* conf->override */;
287
288         return len;
289 }
290
291 int pb_protocol_config_len(const struct config *config)
292 {
293         unsigned int i, len;
294
295         len =   4 /* config->autoboot_enabled */ +
296                 4 /* config->autoboot_timeout_sec */ +
297                 4 /* config->safe_mode */;
298
299         len += 4;
300         for (i = 0; i < config->network.n_interfaces; i++)
301                 len += pb_protocol_interface_config_len(
302                                 config->network.interfaces[i]);
303
304         len += 4;
305         for (i = 0; i < config->network.n_dns_servers; i++)
306                 len += 4 + optional_strlen(config->network.dns_servers[i]);
307
308         len += 4 + optional_strlen(config->http_proxy);
309         len += 4 + optional_strlen(config->https_proxy);
310
311         len += 4;
312         for (i = 0; i < config->n_autoboot_opts; i++) {
313                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
314                         len += 4 + 4;
315                 else
316                         len += 4 + 4 +
317                                 optional_strlen(config->autoboot_opts[i].uuid);
318         }
319
320         len += 4 + 4; /* ipmi_bootdev, ipmi_bootdev_persistent */
321
322         len += 4; /* allow_writes */
323
324         len += 4; /* n_consoles */
325         for (i = 0; i < config->n_consoles; i++)
326                 len += 4 + optional_strlen(config->consoles[i]);
327
328         len += 4 + optional_strlen(config->boot_console);
329         len += 4; /* manual_console */
330
331         len += 4 + optional_strlen(config->lang);
332
333         return len;
334 }
335
336 int pb_protocol_url_len(const char *url)
337 {
338         /* url + length field */
339         return 4 + optional_strlen(url);
340 }
341
342 int pb_protocol_serialise_device(const struct device *dev,
343                 char *buf, int buf_len)
344 {
345         char *pos = buf;
346
347         pos += pb_protocol_serialise_string(pos, dev->id);
348         *(enum device_type *)pos = dev->type;
349         pos += sizeof(enum device_type);
350         pos += pb_protocol_serialise_string(pos, dev->name);
351         pos += pb_protocol_serialise_string(pos, dev->description);
352         pos += pb_protocol_serialise_string(pos, dev->icon_file);
353
354         assert(pos <= buf + buf_len);
355         (void)buf_len;
356
357         return 0;
358 }
359
360 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
361                 char *buf, int buf_len)
362 {
363         char *pos = buf;
364
365         pos += pb_protocol_serialise_string(pos, opt->device_id);
366         pos += pb_protocol_serialise_string(pos, opt->id);
367         pos += pb_protocol_serialise_string(pos, opt->name);
368         pos += pb_protocol_serialise_string(pos, opt->description);
369         pos += pb_protocol_serialise_string(pos, opt->icon_file);
370         pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
371         pos += pb_protocol_serialise_string(pos, opt->initrd_file);
372         pos += pb_protocol_serialise_string(pos, opt->dtb_file);
373         pos += pb_protocol_serialise_string(pos, opt->boot_args);
374         pos += pb_protocol_serialise_string(pos, opt->args_sig_file);
375
376         *(bool *)pos = opt->is_default;
377         pos += sizeof(bool);
378
379         assert(pos <= buf + buf_len);
380         (void)buf_len;
381
382         return 0;
383 }
384
385 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
386                 char *buf, int buf_len)
387 {
388         char *pos = buf;
389
390         pos += pb_protocol_serialise_string(pos, boot->option_id);
391         pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
392         pos += pb_protocol_serialise_string(pos, boot->initrd_file);
393         pos += pb_protocol_serialise_string(pos, boot->dtb_file);
394         pos += pb_protocol_serialise_string(pos, boot->boot_args);
395         pos += pb_protocol_serialise_string(pos, boot->args_sig_file);
396         pos += pb_protocol_serialise_string(pos, boot->console);
397
398         assert(pos <= buf + buf_len);
399         (void)buf_len;
400
401         return 0;
402 }
403
404 int pb_protocol_serialise_boot_status(const struct boot_status *status,
405                 char *buf, int buf_len)
406 {
407         char *pos = buf;
408
409         *(uint32_t *)pos = __cpu_to_be32(status->type);
410         pos += sizeof(uint32_t);
411
412         pos += pb_protocol_serialise_string(pos, status->message);
413         pos += pb_protocol_serialise_string(pos, status->detail);
414
415         *(uint32_t *)pos = __cpu_to_be32(status->type);
416         pos += sizeof(uint32_t);
417
418         assert(pos <= buf + buf_len);
419         (void)buf_len;
420
421         return 0;
422 }
423
424 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
425                 char *buf, int buf_len)
426 {
427         char *pos = buf;
428         unsigned int i;
429
430         pos += pb_protocol_serialise_string(pos, sysinfo->type);
431         pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
432
433         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_primary);
434         pos += sizeof(uint32_t);
435         for (i = 0; i < sysinfo->n_primary; i++)
436                 pos += pb_protocol_serialise_string(pos, sysinfo->platform_primary[i]);
437
438         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_other);
439         pos += sizeof(uint32_t);
440         for (i = 0; i < sysinfo->n_other; i++)
441                 pos += pb_protocol_serialise_string(pos, sysinfo->platform_other[i]);
442
443         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_current);
444         pos += sizeof(uint32_t);
445         for (i = 0; i < sysinfo->n_bmc_current; i++)
446                 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_current[i]);
447
448         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_golden);
449         pos += sizeof(uint32_t);
450         for (i = 0; i < sysinfo->n_bmc_golden; i++)
451                 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_golden[i]);
452
453         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
454         pos += sizeof(uint32_t);
455
456         for (i = 0; i < sysinfo->n_interfaces; i++) {
457                 struct interface_info *if_info = sysinfo->interfaces[i];
458
459                 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
460                 pos += sizeof(uint32_t);
461
462                 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
463                 pos += if_info->hwaddr_size;
464
465                 pos += pb_protocol_serialise_string(pos, if_info->name);
466
467                 *(bool *)pos = if_info->link;
468                 pos += sizeof(bool);
469         }
470
471         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
472         pos += sizeof(uint32_t);
473
474         for (i = 0; i < sysinfo->n_blockdevs; i++) {
475                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
476
477                 pos += pb_protocol_serialise_string(pos, bd_info->name);
478                 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
479                 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
480         }
481
482         if (sysinfo->bmc_mac)
483                 memcpy(pos, sysinfo->bmc_mac, HWADDR_SIZE);
484         else
485                 memset(pos, 0, HWADDR_SIZE);
486         pos += HWADDR_SIZE;
487
488         assert(pos <= buf + buf_len);
489         (void)buf_len;
490
491         return 0;
492 }
493
494 static int pb_protocol_serialise_config_interface(char *buf,
495                 struct interface_config *conf)
496 {
497         char *pos = buf;
498
499         memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
500         pos += sizeof(conf->hwaddr);
501
502         *(uint32_t *)pos = conf->ignore;
503         pos += 4;
504
505         if (conf->ignore)
506                 return pos - buf;
507
508         *(uint32_t *)pos = __cpu_to_be32(conf->method);
509         pos += 4;
510
511         if (conf->method == CONFIG_METHOD_STATIC) {
512                 pos += pb_protocol_serialise_string(pos,
513                                 conf->static_config.address);
514                 pos += pb_protocol_serialise_string(pos,
515                                 conf->static_config.gateway);
516                 pos += pb_protocol_serialise_string(pos,
517                                 conf->static_config.url);
518         }
519
520         *(uint32_t *)pos = conf->override;
521         pos += 4;
522
523         return pos - buf;
524 }
525
526 int pb_protocol_serialise_config(const struct config *config,
527                 char *buf, int buf_len)
528 {
529         char *pos = buf;
530         unsigned int i;
531
532         *(uint32_t *)pos = config->autoboot_enabled;
533         pos += 4;
534
535         *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
536         pos += 4;
537
538         *(uint32_t *)pos = config->safe_mode;
539         pos += 4;
540
541         *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
542         pos += 4;
543         for (i = 0; i < config->network.n_interfaces; i++) {
544                 struct interface_config *iface =
545                         config->network.interfaces[i];
546                 pos += pb_protocol_serialise_config_interface(pos, iface);
547         }
548
549         *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
550         pos += 4;
551         for (i = 0; i < config->network.n_dns_servers; i++) {
552                 pos += pb_protocol_serialise_string(pos,
553                                 config->network.dns_servers[i]);
554         }
555
556         pos += pb_protocol_serialise_string(pos, config->http_proxy);
557         pos += pb_protocol_serialise_string(pos, config->https_proxy);
558
559         *(uint32_t *)pos = __cpu_to_be32(config->n_autoboot_opts);
560         pos += 4;
561         for (i = 0; i < config->n_autoboot_opts; i++) {
562                 *(uint32_t *)pos =
563                         __cpu_to_be32(config->autoboot_opts[i].boot_type);
564                 pos += 4;
565                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
566                         *(uint32_t *)pos =
567                                 __cpu_to_be32(config->autoboot_opts[i].type);
568                         pos += 4;
569                 } else {
570                         pos += pb_protocol_serialise_string(pos,
571                                                 config->autoboot_opts[i].uuid);
572                 }
573         }
574
575         *(uint32_t *)pos = __cpu_to_be32(config->ipmi_bootdev);
576         pos += 4;
577         *(uint32_t *)pos = config->ipmi_bootdev_persistent;
578         pos += 4;
579
580         *(uint32_t *)pos = config->allow_writes;
581         pos += 4;
582
583         *(uint32_t *)pos = __cpu_to_be32(config->n_consoles);
584         pos += 4;
585         for (i = 0; i < config->n_consoles; i++)
586                 pos += pb_protocol_serialise_string(pos, config->consoles[i]);
587
588         pos += pb_protocol_serialise_string(pos, config->boot_console);
589         *(uint32_t *)pos = config->manual_console;
590         pos += 4;
591
592         pos += pb_protocol_serialise_string(pos, config->lang);
593
594         assert(pos <= buf + buf_len);
595         (void)buf_len;
596
597         return 0;
598 }
599
600 int pb_protocol_serialise_url(const char *url, char *buf, int buf_len)
601 {
602         char *pos = buf;
603
604         pos += pb_protocol_serialise_string(pos, url);
605
606         assert(pos <=buf+buf_len);
607         (void)buf_len;
608
609         return 0;
610 }
611
612 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
613 {
614         int total_len, rc;
615         char *pos;
616
617         total_len = sizeof(*message) + message->payload_len;
618
619         message->payload_len = __cpu_to_be32(message->payload_len);
620         message->action = __cpu_to_be32(message->action);
621
622         for (pos = (void *)message; total_len;) {
623                 rc = write(fd, pos, total_len);
624
625                 if (rc <= 0)
626                         break;
627
628                 total_len -= rc;
629                 pos += rc;
630         }
631
632         talloc_free(message);
633
634         if (!total_len)
635                 return 0;
636
637         pb_log("%s: failed: %s\n", __func__, strerror(errno));
638         return -1;
639 }
640
641 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
642                 enum pb_protocol_action action, int payload_len)
643 {
644         struct pb_protocol_message *message;
645
646         if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
647                 pb_log("%s: payload too big %u/%u\n", __func__, payload_len,
648                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
649                 return NULL;
650         }
651
652         message = talloc_size(ctx, sizeof(*message) + payload_len);
653
654         /* we convert these to big-endian in write_message() */
655         message->action = action;
656         message->payload_len = payload_len;
657
658         return message;
659
660 }
661
662 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
663 {
664         struct pb_protocol_message *message, m;
665         int rc;
666         unsigned int len;
667
668         /* use the stack for the initial 8-byte read */
669
670         rc = read(fd, &m, sizeof(m));
671         if (rc != sizeof(m))
672                 return NULL;
673
674         m.payload_len = __be32_to_cpu(m.payload_len);
675         m.action = __be32_to_cpu(m.action);
676
677         if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
678                 pb_log("%s: payload too big %u/%u\n", __func__, m.payload_len,
679                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
680                 return NULL;
681         }
682
683         message = talloc_size(ctx, sizeof(m) + m.payload_len);
684         memcpy(message, &m, sizeof(m));
685
686         for (len = 0; len < m.payload_len;) {
687                 rc = read(fd, message->payload + len, m.payload_len - len);
688
689                 if (rc <= 0) {
690                         talloc_free(message);
691                         pb_log("%s: failed (%u): %s\n", __func__, len,
692                                 strerror(errno));
693                         return NULL;
694                 }
695
696                 len += rc;
697         }
698
699         return message;
700 }
701
702
703 int pb_protocol_deserialise_device(struct device *dev,
704                 const struct pb_protocol_message *message)
705 {
706         unsigned int len;
707         const char *pos;
708         int rc = -1;
709
710         len = message->payload_len;
711         pos = message->payload;
712
713         if (read_string(dev, &pos, &len, &dev->id))
714                 goto out;
715
716         if (len < sizeof(enum device_type))
717                 goto out;
718         dev->type = *(enum device_type *)(pos);
719         pos += sizeof(enum device_type);
720         len -= sizeof(enum device_type);
721
722         if (read_string(dev, &pos, &len, &dev->name))
723                 goto out;
724
725         if (read_string(dev, &pos, &len, &dev->description))
726                 goto out;
727
728         if (read_string(dev, &pos, &len, &dev->icon_file))
729                 goto out;
730
731         rc = 0;
732
733 out:
734         return rc;
735 }
736
737 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
738                 const struct pb_protocol_message *message)
739 {
740         unsigned int len;
741         const char *pos;
742         int rc = -1;
743
744         len = message->payload_len;
745         pos = message->payload;
746
747         if (read_string(opt, &pos, &len, &opt->device_id))
748                 goto out;
749
750         if (read_string(opt, &pos, &len, &opt->id))
751                 goto out;
752
753         if (read_string(opt, &pos, &len, &opt->name))
754                 goto out;
755
756         if (read_string(opt, &pos, &len, &opt->description))
757                 goto out;
758
759         if (read_string(opt, &pos, &len, &opt->icon_file))
760                 goto out;
761
762         if (read_string(opt, &pos, &len, &opt->boot_image_file))
763                 goto out;
764
765         if (read_string(opt, &pos, &len, &opt->initrd_file))
766                 goto out;
767
768         if (read_string(opt, &pos, &len, &opt->dtb_file))
769                 goto out;
770
771         if (read_string(opt, &pos, &len, &opt->boot_args))
772                 goto out;
773
774         if (read_string(opt, &pos, &len, &opt->args_sig_file))
775                 goto out;
776
777         if (len < sizeof(bool))
778                 goto out;
779         opt->is_default = *(bool *)(pos);
780
781         rc = 0;
782
783 out:
784         return rc;
785 }
786
787 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
788                 const struct pb_protocol_message *message)
789 {
790         unsigned int len;
791         const char *pos;
792         int rc = -1;
793
794         len = message->payload_len;
795         pos = message->payload;
796
797         if (read_string(cmd, &pos, &len, &cmd->option_id))
798                 goto out;
799
800         if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
801                 goto out;
802
803         if (read_string(cmd, &pos, &len, &cmd->initrd_file))
804                 goto out;
805
806         if (read_string(cmd, &pos, &len, &cmd->dtb_file))
807                 goto out;
808
809         if (read_string(cmd, &pos, &len, &cmd->boot_args))
810                 goto out;
811
812         if (read_string(cmd, &pos, &len, &cmd->args_sig_file))
813                 goto out;
814
815         if (read_string(cmd, &pos, &len, &cmd->console))
816                 goto out;
817
818         rc = 0;
819
820 out:
821         return rc;
822 }
823
824 int pb_protocol_deserialise_boot_status(struct boot_status *status,
825                 const struct pb_protocol_message *message)
826 {
827         unsigned int len;
828         const char *pos;
829         int rc = -1;
830
831         len = message->payload_len;
832         pos = message->payload;
833
834         /* first up, the type enum... */
835         if (len < sizeof(uint32_t))
836                 goto out;
837
838         status->type = __be32_to_cpu(*(uint32_t *)(pos));
839
840         switch (status->type) {
841         case BOOT_STATUS_ERROR:
842         case BOOT_STATUS_INFO:
843                 break;
844         default:
845                 goto out;
846         }
847
848         pos += sizeof(uint32_t);
849         len -= sizeof(uint32_t);
850
851         /* message and detail strings */
852         if (read_string(status, &pos, &len, &status->message))
853                 goto out;
854
855         if (read_string(status, &pos, &len, &status->detail))
856                 goto out;
857
858         /* and finally, progress */
859         if (len < sizeof(uint32_t))
860                 goto out;
861
862         status->progress = __be32_to_cpu(*(uint32_t *)(pos));
863
864         /* clamp to 100% */
865         if (status->progress > 100)
866                 status->progress = 100;
867
868         rc = 0;
869
870 out:
871         return rc;
872 }
873
874 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
875                 const struct pb_protocol_message *message)
876 {
877         unsigned int len, i;
878         const char *pos;
879         int rc = -1;
880         char *tmp;
881
882         len = message->payload_len;
883         pos = message->payload;
884
885         /* type and identifier strings */
886         if (read_string(sysinfo, &pos, &len, &sysinfo->type))
887                 goto out;
888
889         if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
890                 goto out;
891
892         /* Platform version strings for openpower platforms */
893         if (read_u32(&pos, &len, &sysinfo->n_primary))
894                 goto out;
895         sysinfo->platform_primary = talloc_array(sysinfo, char *,
896                                                 sysinfo->n_primary);
897         for (i = 0; i < sysinfo->n_primary; i++) {
898                 if (read_string(sysinfo, &pos, &len, &tmp))
899                         goto out;
900                 sysinfo->platform_primary[i] = talloc_strdup(sysinfo, tmp);
901         }
902
903         if (read_u32(&pos, &len, &sysinfo->n_other))
904                 goto out;
905         sysinfo->platform_other = talloc_array(sysinfo, char *,
906                                                 sysinfo->n_other);
907         for (i = 0; i < sysinfo->n_other; i++) {
908                 if (read_string(sysinfo, &pos, &len, &tmp))
909                         goto out;
910                 sysinfo->platform_other[i] = talloc_strdup(sysinfo, tmp);
911         }
912
913         /* BMC version strings for openpower platforms */
914         if (read_u32(&pos, &len, &sysinfo->n_bmc_current))
915                 goto out;
916         sysinfo->bmc_current = talloc_array(sysinfo, char *,
917                                                 sysinfo->n_bmc_current);
918         for (i = 0; i < sysinfo->n_bmc_current; i++) {
919                 if (read_string(sysinfo, &pos, &len, &tmp))
920                         goto out;
921                 sysinfo->bmc_current[i] = talloc_strdup(sysinfo, tmp);
922         }
923
924         if (read_u32(&pos, &len, &sysinfo->n_bmc_golden))
925                 goto out;
926         sysinfo->bmc_golden = talloc_array(sysinfo, char *,
927                                                 sysinfo->n_bmc_golden);
928         for (i = 0; i < sysinfo->n_bmc_golden; i++) {
929                 if (read_string(sysinfo, &pos, &len, &tmp))
930                         goto out;
931                 sysinfo->bmc_golden[i] = talloc_strdup(sysinfo, tmp);
932         }
933
934         /* number of interfaces */
935         if (read_u32(&pos, &len, &sysinfo->n_interfaces))
936                 goto out;
937
938         sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
939                         sysinfo->n_interfaces);
940
941         for (i = 0; i < sysinfo->n_interfaces; i++) {
942                 struct interface_info *if_info = talloc(sysinfo,
943                                                         struct interface_info);
944
945                 if (read_u32(&pos, &len, &if_info->hwaddr_size))
946                         goto out;
947
948                 if (len < if_info->hwaddr_size)
949                         goto out;
950
951                 if_info->hwaddr = talloc_memdup(if_info, pos,
952                                                 if_info->hwaddr_size);
953                 pos += if_info->hwaddr_size;
954                 len -= if_info->hwaddr_size;
955
956                 if (read_string(if_info, &pos, &len, &if_info->name))
957                         goto out;
958
959                 if_info->link = *(bool *)pos;
960                 pos += sizeof(if_info->link);
961
962                 sysinfo->interfaces[i] = if_info;
963         }
964
965         /* number of interfaces */
966         if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
967                 goto out;
968
969         sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
970                         sysinfo->n_blockdevs);
971
972         for (i = 0; i < sysinfo->n_blockdevs; i++) {
973                 struct blockdev_info *bd_info = talloc(sysinfo,
974                                                         struct blockdev_info);
975
976                 if (read_string(bd_info, &pos, &len, &bd_info->name))
977                         goto out;
978
979                 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
980                         goto out;
981
982                 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
983                         goto out;
984
985                 sysinfo->blockdevs[i] = bd_info;
986         }
987
988         for (i = 0; i < HWADDR_SIZE; i++) {
989                 if (pos[i] != 0) {
990                         sysinfo->bmc_mac = talloc_memdup(sysinfo, pos, HWADDR_SIZE);
991                         break;
992                 }
993         }
994
995         pos += HWADDR_SIZE;
996         len -= HWADDR_SIZE;
997
998         rc = 0;
999 out:
1000         return rc;
1001 }
1002
1003 static int pb_protocol_deserialise_config_interface(const char **buf,
1004                 unsigned int *len, struct interface_config *iface)
1005 {
1006         unsigned int tmp;
1007
1008         if (*len < sizeof(iface->hwaddr))
1009                 return -1;
1010
1011         memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
1012         *buf += sizeof(iface->hwaddr);
1013         *len -= sizeof(iface->hwaddr);
1014
1015         if (read_u32(buf, len, &tmp))
1016                 return -1;
1017         iface->ignore = !!tmp;
1018
1019         if (iface->ignore)
1020                 return 0;
1021
1022         if (read_u32(buf, len, &iface->method))
1023                 return -1;
1024
1025         if (iface->method == CONFIG_METHOD_STATIC) {
1026                 if (read_string(iface, buf, len, &iface->static_config.address))
1027                         return -1;
1028
1029                 if (read_string(iface, buf, len, &iface->static_config.gateway))
1030                         return -1;
1031
1032                 if (read_string(iface, buf, len, &iface->static_config.url))
1033                         return -1;
1034         }
1035
1036         if (read_u32(buf, len, &tmp))
1037                 return -1;
1038         iface->override = !!tmp;
1039
1040         return 0;
1041 }
1042
1043 int pb_protocol_deserialise_config(struct config *config,
1044                 const struct pb_protocol_message *message)
1045 {
1046         unsigned int len, i, tmp;
1047         const char *pos;
1048         int rc = -1;
1049         char *str;
1050
1051         len = message->payload_len;
1052         pos = message->payload;
1053
1054         if (read_u32(&pos, &len, &tmp))
1055                 goto out;
1056         config->autoboot_enabled = !!tmp;
1057
1058         if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
1059                 goto out;
1060
1061         if (read_u32(&pos, &len, &tmp))
1062                 goto out;
1063         config->safe_mode = !!tmp;
1064
1065         if (read_u32(&pos, &len, &config->network.n_interfaces))
1066                 goto out;
1067
1068         config->network.interfaces = talloc_array(config,
1069                         struct interface_config *, config->network.n_interfaces);
1070
1071         for (i = 0; i < config->network.n_interfaces; i++) {
1072                 struct interface_config *iface = talloc_zero(
1073                                 config->network.interfaces,
1074                                 struct interface_config);
1075                 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
1076                         goto out;
1077                 config->network.interfaces[i] = iface;
1078         }
1079
1080         if (read_u32(&pos, &len, &config->network.n_dns_servers))
1081                 goto out;
1082         config->network.dns_servers = talloc_array(config, const char *,
1083                         config->network.n_dns_servers);
1084
1085         for (i = 0; i < config->network.n_dns_servers; i++) {
1086                 if (read_string(config->network.dns_servers, &pos, &len, &str))
1087                         goto out;
1088                 config->network.dns_servers[i] = str;
1089         }
1090
1091         if (read_string(config, &pos, &len, &str))
1092                 goto out;
1093         config->http_proxy = str;
1094         if (read_string(config, &pos, &len, &str))
1095                 goto out;
1096         config->https_proxy = str;
1097
1098         if (read_u32(&pos, &len, &config->n_autoboot_opts))
1099                 goto out;
1100         config->autoboot_opts = talloc_array(config, struct autoboot_option,
1101                         config->n_autoboot_opts);
1102
1103         for (i = 0; i < config->n_autoboot_opts; i++) {
1104                 if (read_u32(&pos, &len, &tmp))
1105                         goto out;
1106                 config->autoboot_opts[i].boot_type = (int)tmp;
1107                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
1108                         if (read_u32(&pos, &len, &tmp))
1109                                 goto out;
1110                         config->autoboot_opts[i].type = tmp;
1111                 } else {
1112                         if (read_string(config, &pos, &len, &str))
1113                                 goto out;
1114                         config->autoboot_opts[i].uuid = str;
1115                 }
1116         }
1117
1118         if (read_u32(&pos, &len, &config->ipmi_bootdev))
1119                 goto out;
1120         if (read_u32(&pos, &len, &tmp))
1121                 goto out;
1122         config->ipmi_bootdev_persistent = !!tmp;
1123
1124         if (read_u32(&pos, &len, &tmp))
1125                 goto out;
1126         config->allow_writes = !!tmp;
1127
1128         if (read_u32(&pos, &len, &config->n_consoles))
1129                 goto out;
1130
1131         config->consoles = talloc_array(config, char *, config->n_consoles);
1132         for (i = 0; i < config->n_consoles; i++) {
1133                 if (read_string(config->consoles, &pos, &len, &str))
1134                         goto out;
1135                 config->consoles[i] = str;
1136         }
1137
1138         if (read_string(config, &pos, &len, &str))
1139                 goto out;
1140
1141         config->boot_console = str;
1142
1143         if (read_u32(&pos, &len, &tmp))
1144                 goto out;
1145         config->manual_console = !!tmp;
1146
1147         if (read_string(config, &pos, &len, &str))
1148                 goto out;
1149
1150         config->lang = str;
1151
1152         rc = 0;
1153
1154 out:
1155         return rc;
1156 }