Add support for GPG signature enforcement on booted
[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->tty);
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         return len;
287 }
288
289 int pb_protocol_config_len(const struct config *config)
290 {
291         unsigned int i, len;
292
293         len =   4 /* config->autoboot_enabled */ +
294                 4 /* config->autoboot_timeout_sec */ +
295                 4 /* config->safe_mode */;
296
297         len += 4;
298         for (i = 0; i < config->network.n_interfaces; i++)
299                 len += pb_protocol_interface_config_len(
300                                 config->network.interfaces[i]);
301
302         len += 4;
303         for (i = 0; i < config->network.n_dns_servers; i++)
304                 len += 4 + optional_strlen(config->network.dns_servers[i]);
305
306         len += 4;
307         for (i = 0; i < config->n_autoboot_opts; i++) {
308                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
309                         len += 4 + 4;
310                 else
311                         len += 4 + 4 +
312                                 optional_strlen(config->autoboot_opts[i].uuid);
313         }
314
315         len += 4 + 4; /* ipmi_bootdev, ipmi_bootdev_persistent */
316
317         len += 4; /* allow_writes */
318
319         len += 4; /* n_tty */
320         for (i = 0; i < config->n_tty; i++)
321                 len += 4 + optional_strlen(config->tty_list[i]);
322
323         len += 4 + optional_strlen(config->boot_tty);
324
325         len += 4 + optional_strlen(config->lang);
326
327         return len;
328 }
329
330 int pb_protocol_url_len(const char *url)
331 {
332         /* url + length field */
333         return 4 + optional_strlen(url);
334 }
335
336 int pb_protocol_serialise_device(const struct device *dev,
337                 char *buf, int buf_len)
338 {
339         char *pos = buf;
340
341         pos += pb_protocol_serialise_string(pos, dev->id);
342         *(enum device_type *)pos = dev->type;
343         pos += sizeof(enum device_type);
344         pos += pb_protocol_serialise_string(pos, dev->name);
345         pos += pb_protocol_serialise_string(pos, dev->description);
346         pos += pb_protocol_serialise_string(pos, dev->icon_file);
347
348         assert(pos <= buf + buf_len);
349         (void)buf_len;
350
351         return 0;
352 }
353
354 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
355                 char *buf, int buf_len)
356 {
357         char *pos = buf;
358
359         pos += pb_protocol_serialise_string(pos, opt->device_id);
360         pos += pb_protocol_serialise_string(pos, opt->id);
361         pos += pb_protocol_serialise_string(pos, opt->name);
362         pos += pb_protocol_serialise_string(pos, opt->description);
363         pos += pb_protocol_serialise_string(pos, opt->icon_file);
364         pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
365         pos += pb_protocol_serialise_string(pos, opt->initrd_file);
366         pos += pb_protocol_serialise_string(pos, opt->dtb_file);
367         pos += pb_protocol_serialise_string(pos, opt->boot_args);
368         pos += pb_protocol_serialise_string(pos, opt->args_sig_file);
369
370         *(bool *)pos = opt->is_default;
371         pos += sizeof(bool);
372
373         assert(pos <= buf + buf_len);
374         (void)buf_len;
375
376         return 0;
377 }
378
379 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
380                 char *buf, int buf_len)
381 {
382         char *pos = buf;
383
384         pos += pb_protocol_serialise_string(pos, boot->option_id);
385         pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
386         pos += pb_protocol_serialise_string(pos, boot->initrd_file);
387         pos += pb_protocol_serialise_string(pos, boot->dtb_file);
388         pos += pb_protocol_serialise_string(pos, boot->boot_args);
389         pos += pb_protocol_serialise_string(pos, boot->args_sig_file);
390         pos += pb_protocol_serialise_string(pos, boot->tty);
391
392         assert(pos <= buf + buf_len);
393         (void)buf_len;
394
395         return 0;
396 }
397
398 int pb_protocol_serialise_boot_status(const struct boot_status *status,
399                 char *buf, int buf_len)
400 {
401         char *pos = buf;
402
403         *(uint32_t *)pos = __cpu_to_be32(status->type);
404         pos += sizeof(uint32_t);
405
406         pos += pb_protocol_serialise_string(pos, status->message);
407         pos += pb_protocol_serialise_string(pos, status->detail);
408
409         *(uint32_t *)pos = __cpu_to_be32(status->type);
410         pos += sizeof(uint32_t);
411
412         assert(pos <= buf + buf_len);
413         (void)buf_len;
414
415         return 0;
416 }
417
418 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
419                 char *buf, int buf_len)
420 {
421         char *pos = buf;
422         unsigned int i;
423
424         pos += pb_protocol_serialise_string(pos, sysinfo->type);
425         pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
426
427         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_primary);
428         pos += sizeof(uint32_t);
429         for (i = 0; i < sysinfo->n_primary; i++)
430                 pos += pb_protocol_serialise_string(pos, sysinfo->platform_primary[i]);
431
432         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_other);
433         pos += sizeof(uint32_t);
434         for (i = 0; i < sysinfo->n_other; i++)
435                 pos += pb_protocol_serialise_string(pos, sysinfo->platform_other[i]);
436
437         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_current);
438         pos += sizeof(uint32_t);
439         for (i = 0; i < sysinfo->n_bmc_current; i++)
440                 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_current[i]);
441
442         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_golden);
443         pos += sizeof(uint32_t);
444         for (i = 0; i < sysinfo->n_bmc_golden; i++)
445                 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_golden[i]);
446
447         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
448         pos += sizeof(uint32_t);
449
450         for (i = 0; i < sysinfo->n_interfaces; i++) {
451                 struct interface_info *if_info = sysinfo->interfaces[i];
452
453                 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
454                 pos += sizeof(uint32_t);
455
456                 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
457                 pos += if_info->hwaddr_size;
458
459                 pos += pb_protocol_serialise_string(pos, if_info->name);
460
461                 *(bool *)pos = if_info->link;
462                 pos += sizeof(bool);
463         }
464
465         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
466         pos += sizeof(uint32_t);
467
468         for (i = 0; i < sysinfo->n_blockdevs; i++) {
469                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
470
471                 pos += pb_protocol_serialise_string(pos, bd_info->name);
472                 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
473                 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
474         }
475
476         if (sysinfo->bmc_mac)
477                 memcpy(pos, sysinfo->bmc_mac, HWADDR_SIZE);
478         else
479                 memset(pos, 0, HWADDR_SIZE);
480         pos += HWADDR_SIZE;
481
482         assert(pos <= buf + buf_len);
483         (void)buf_len;
484
485         return 0;
486 }
487
488 static int pb_protocol_serialise_config_interface(char *buf,
489                 struct interface_config *conf)
490 {
491         char *pos = buf;
492
493         memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
494         pos += sizeof(conf->hwaddr);
495
496         *(uint32_t *)pos = conf->ignore;
497         pos += 4;
498
499         if (conf->ignore)
500                 return pos - buf;
501
502         *(uint32_t *)pos = __cpu_to_be32(conf->method);
503         pos += 4;
504
505         if (conf->method == CONFIG_METHOD_STATIC) {
506                 pos += pb_protocol_serialise_string(pos,
507                                 conf->static_config.address);
508                 pos += pb_protocol_serialise_string(pos,
509                                 conf->static_config.gateway);
510                 pos += pb_protocol_serialise_string(pos,
511                                 conf->static_config.url);
512         }
513
514         return pos - buf;
515 }
516
517 int pb_protocol_serialise_config(const struct config *config,
518                 char *buf, int buf_len)
519 {
520         char *pos = buf;
521         unsigned int i;
522
523         *(uint32_t *)pos = config->autoboot_enabled;
524         pos += 4;
525
526         *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
527         pos += 4;
528
529         *(uint32_t *)pos = config->safe_mode;
530         pos += 4;
531
532         *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
533         pos += 4;
534         for (i = 0; i < config->network.n_interfaces; i++) {
535                 struct interface_config *iface =
536                         config->network.interfaces[i];
537                 pos += pb_protocol_serialise_config_interface(pos, iface);
538         }
539
540         *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
541         pos += 4;
542         for (i = 0; i < config->network.n_dns_servers; i++) {
543                 pos += pb_protocol_serialise_string(pos,
544                                 config->network.dns_servers[i]);
545         }
546
547         *(uint32_t *)pos = __cpu_to_be32(config->n_autoboot_opts);
548         pos += 4;
549         for (i = 0; i < config->n_autoboot_opts; i++) {
550                 *(uint32_t *)pos =
551                         __cpu_to_be32(config->autoboot_opts[i].boot_type);
552                 pos += 4;
553                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
554                         *(uint32_t *)pos =
555                                 __cpu_to_be32(config->autoboot_opts[i].type);
556                         pos += 4;
557                 } else {
558                         pos += pb_protocol_serialise_string(pos,
559                                                 config->autoboot_opts[i].uuid);
560                 }
561         }
562
563         *(uint32_t *)pos = __cpu_to_be32(config->ipmi_bootdev);
564         pos += 4;
565         *(uint32_t *)pos = config->ipmi_bootdev_persistent;
566         pos += 4;
567
568         *(uint32_t *)pos = config->allow_writes;
569         pos += 4;
570
571         *(uint32_t *)pos = __cpu_to_be32(config->n_tty);
572         pos += 4;
573         for (i = 0; i < config->n_tty; i++)
574                 pos += pb_protocol_serialise_string(pos, config->tty_list[i]);
575
576         pos += pb_protocol_serialise_string(pos, config->boot_tty);
577
578         pos += pb_protocol_serialise_string(pos, config->lang);
579
580         assert(pos <= buf + buf_len);
581         (void)buf_len;
582
583         return 0;
584 }
585
586 int pb_protocol_serialise_url(const char *url, char *buf, int buf_len)
587 {
588         char *pos = buf;
589
590         pos += pb_protocol_serialise_string(pos, url);
591
592         assert(pos <=buf+buf_len);
593         (void)buf_len;
594
595         return 0;
596 }
597
598 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
599 {
600         int total_len, rc;
601         char *pos;
602
603         total_len = sizeof(*message) + message->payload_len;
604
605         message->payload_len = __cpu_to_be32(message->payload_len);
606         message->action = __cpu_to_be32(message->action);
607
608         for (pos = (void *)message; total_len;) {
609                 rc = write(fd, pos, total_len);
610
611                 if (rc <= 0)
612                         break;
613
614                 total_len -= rc;
615                 pos += rc;
616         }
617
618         talloc_free(message);
619
620         if (!total_len)
621                 return 0;
622
623         pb_log("%s: failed: %s\n", __func__, strerror(errno));
624         return -1;
625 }
626
627 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
628                 enum pb_protocol_action action, int payload_len)
629 {
630         struct pb_protocol_message *message;
631
632         if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
633                 pb_log("%s: payload too big %u/%u\n", __func__, payload_len,
634                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
635                 return NULL;
636         }
637
638         message = talloc_size(ctx, sizeof(*message) + payload_len);
639
640         /* we convert these to big-endian in write_message() */
641         message->action = action;
642         message->payload_len = payload_len;
643
644         return message;
645
646 }
647
648 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
649 {
650         struct pb_protocol_message *message, m;
651         int rc;
652         unsigned int len;
653
654         /* use the stack for the initial 8-byte read */
655
656         rc = read(fd, &m, sizeof(m));
657         if (rc != sizeof(m))
658                 return NULL;
659
660         m.payload_len = __be32_to_cpu(m.payload_len);
661         m.action = __be32_to_cpu(m.action);
662
663         if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
664                 pb_log("%s: payload too big %u/%u\n", __func__, m.payload_len,
665                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
666                 return NULL;
667         }
668
669         message = talloc_size(ctx, sizeof(m) + m.payload_len);
670         memcpy(message, &m, sizeof(m));
671
672         for (len = 0; len < m.payload_len;) {
673                 rc = read(fd, message->payload + len, m.payload_len - len);
674
675                 if (rc <= 0) {
676                         talloc_free(message);
677                         pb_log("%s: failed (%u): %s\n", __func__, len,
678                                 strerror(errno));
679                         return NULL;
680                 }
681
682                 len += rc;
683         }
684
685         return message;
686 }
687
688
689 int pb_protocol_deserialise_device(struct device *dev,
690                 const struct pb_protocol_message *message)
691 {
692         unsigned int len;
693         const char *pos;
694         int rc = -1;
695
696         len = message->payload_len;
697         pos = message->payload;
698
699         if (read_string(dev, &pos, &len, &dev->id))
700                 goto out;
701
702         if (len < sizeof(enum device_type))
703                 goto out;
704         dev->type = *(enum device_type *)(pos);
705         pos += sizeof(enum device_type);
706         len -= sizeof(enum device_type);
707
708         if (read_string(dev, &pos, &len, &dev->name))
709                 goto out;
710
711         if (read_string(dev, &pos, &len, &dev->description))
712                 goto out;
713
714         if (read_string(dev, &pos, &len, &dev->icon_file))
715                 goto out;
716
717         rc = 0;
718
719 out:
720         return rc;
721 }
722
723 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
724                 const struct pb_protocol_message *message)
725 {
726         unsigned int len;
727         const char *pos;
728         int rc = -1;
729
730         len = message->payload_len;
731         pos = message->payload;
732
733         if (read_string(opt, &pos, &len, &opt->device_id))
734                 goto out;
735
736         if (read_string(opt, &pos, &len, &opt->id))
737                 goto out;
738
739         if (read_string(opt, &pos, &len, &opt->name))
740                 goto out;
741
742         if (read_string(opt, &pos, &len, &opt->description))
743                 goto out;
744
745         if (read_string(opt, &pos, &len, &opt->icon_file))
746                 goto out;
747
748         if (read_string(opt, &pos, &len, &opt->boot_image_file))
749                 goto out;
750
751         if (read_string(opt, &pos, &len, &opt->initrd_file))
752                 goto out;
753
754         if (read_string(opt, &pos, &len, &opt->dtb_file))
755                 goto out;
756
757         if (read_string(opt, &pos, &len, &opt->boot_args))
758                 goto out;
759
760         if (read_string(opt, &pos, &len, &opt->args_sig_file))
761                 goto out;
762
763         if (len < sizeof(bool))
764                 goto out;
765         opt->is_default = *(bool *)(pos);
766
767         rc = 0;
768
769 out:
770         return rc;
771 }
772
773 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
774                 const struct pb_protocol_message *message)
775 {
776         unsigned int len;
777         const char *pos;
778         int rc = -1;
779
780         len = message->payload_len;
781         pos = message->payload;
782
783         if (read_string(cmd, &pos, &len, &cmd->option_id))
784                 goto out;
785
786         if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
787                 goto out;
788
789         if (read_string(cmd, &pos, &len, &cmd->initrd_file))
790                 goto out;
791
792         if (read_string(cmd, &pos, &len, &cmd->dtb_file))
793                 goto out;
794
795         if (read_string(cmd, &pos, &len, &cmd->boot_args))
796                 goto out;
797
798         if (read_string(cmd, &pos, &len, &cmd->args_sig_file))
799                 goto out;
800
801         if (read_string(cmd, &pos, &len, &cmd->tty))
802                 goto out;
803
804         rc = 0;
805
806 out:
807         return rc;
808 }
809
810 int pb_protocol_deserialise_boot_status(struct boot_status *status,
811                 const struct pb_protocol_message *message)
812 {
813         unsigned int len;
814         const char *pos;
815         int rc = -1;
816
817         len = message->payload_len;
818         pos = message->payload;
819
820         /* first up, the type enum... */
821         if (len < sizeof(uint32_t))
822                 goto out;
823
824         status->type = __be32_to_cpu(*(uint32_t *)(pos));
825
826         switch (status->type) {
827         case BOOT_STATUS_ERROR:
828         case BOOT_STATUS_INFO:
829                 break;
830         default:
831                 goto out;
832         }
833
834         pos += sizeof(uint32_t);
835         len -= sizeof(uint32_t);
836
837         /* message and detail strings */
838         if (read_string(status, &pos, &len, &status->message))
839                 goto out;
840
841         if (read_string(status, &pos, &len, &status->detail))
842                 goto out;
843
844         /* and finally, progress */
845         if (len < sizeof(uint32_t))
846                 goto out;
847
848         status->progress = __be32_to_cpu(*(uint32_t *)(pos));
849
850         /* clamp to 100% */
851         if (status->progress > 100)
852                 status->progress = 100;
853
854         rc = 0;
855
856 out:
857         return rc;
858 }
859
860 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
861                 const struct pb_protocol_message *message)
862 {
863         unsigned int len, i;
864         const char *pos;
865         int rc = -1;
866         char *tmp;
867
868         len = message->payload_len;
869         pos = message->payload;
870
871         /* type and identifier strings */
872         if (read_string(sysinfo, &pos, &len, &sysinfo->type))
873                 goto out;
874
875         if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
876                 goto out;
877
878         /* Platform version strings for openpower platforms */
879         if (read_u32(&pos, &len, &sysinfo->n_primary))
880                 goto out;
881         sysinfo->platform_primary = talloc_array(sysinfo, char *,
882                                                 sysinfo->n_primary);
883         for (i = 0; i < sysinfo->n_primary; i++) {
884                 if (read_string(sysinfo, &pos, &len, &tmp))
885                         goto out;
886                 sysinfo->platform_primary[i] = talloc_strdup(sysinfo, tmp);
887         }
888
889         if (read_u32(&pos, &len, &sysinfo->n_other))
890                 goto out;
891         sysinfo->platform_other = talloc_array(sysinfo, char *,
892                                                 sysinfo->n_other);
893         for (i = 0; i < sysinfo->n_other; i++) {
894                 if (read_string(sysinfo, &pos, &len, &tmp))
895                         goto out;
896                 sysinfo->platform_other[i] = talloc_strdup(sysinfo, tmp);
897         }
898
899         /* BMC version strings for openpower platforms */
900         if (read_u32(&pos, &len, &sysinfo->n_bmc_current))
901                 goto out;
902         sysinfo->bmc_current = talloc_array(sysinfo, char *,
903                                                 sysinfo->n_bmc_current);
904         for (i = 0; i < sysinfo->n_bmc_current; i++) {
905                 if (read_string(sysinfo, &pos, &len, &tmp))
906                         goto out;
907                 sysinfo->bmc_current[i] = talloc_strdup(sysinfo, tmp);
908         }
909
910         if (read_u32(&pos, &len, &sysinfo->n_bmc_golden))
911                 goto out;
912         sysinfo->bmc_golden = talloc_array(sysinfo, char *,
913                                                 sysinfo->n_bmc_golden);
914         for (i = 0; i < sysinfo->n_bmc_golden; i++) {
915                 if (read_string(sysinfo, &pos, &len, &tmp))
916                         goto out;
917                 sysinfo->bmc_golden[i] = talloc_strdup(sysinfo, tmp);
918         }
919
920         /* number of interfaces */
921         if (read_u32(&pos, &len, &sysinfo->n_interfaces))
922                 goto out;
923
924         sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
925                         sysinfo->n_interfaces);
926
927         for (i = 0; i < sysinfo->n_interfaces; i++) {
928                 struct interface_info *if_info = talloc(sysinfo,
929                                                         struct interface_info);
930
931                 if (read_u32(&pos, &len, &if_info->hwaddr_size))
932                         goto out;
933
934                 if (len < if_info->hwaddr_size)
935                         goto out;
936
937                 if_info->hwaddr = talloc_memdup(if_info, pos,
938                                                 if_info->hwaddr_size);
939                 pos += if_info->hwaddr_size;
940                 len -= if_info->hwaddr_size;
941
942                 if (read_string(if_info, &pos, &len, &if_info->name))
943                         goto out;
944
945                 if_info->link = *(bool *)pos;
946                 pos += sizeof(if_info->link);
947
948                 sysinfo->interfaces[i] = if_info;
949         }
950
951         /* number of interfaces */
952         if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
953                 goto out;
954
955         sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
956                         sysinfo->n_blockdevs);
957
958         for (i = 0; i < sysinfo->n_blockdevs; i++) {
959                 struct blockdev_info *bd_info = talloc(sysinfo,
960                                                         struct blockdev_info);
961
962                 if (read_string(bd_info, &pos, &len, &bd_info->name))
963                         goto out;
964
965                 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
966                         goto out;
967
968                 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
969                         goto out;
970
971                 sysinfo->blockdevs[i] = bd_info;
972         }
973
974         for (i = 0; i < HWADDR_SIZE; i++) {
975                 if (pos[i] != 0) {
976                         sysinfo->bmc_mac = talloc_memdup(sysinfo, pos, HWADDR_SIZE);
977                         break;
978                 }
979         }
980
981         pos += HWADDR_SIZE;
982         len -= HWADDR_SIZE;
983
984         rc = 0;
985 out:
986         return rc;
987 }
988
989 static int pb_protocol_deserialise_config_interface(const char **buf,
990                 unsigned int *len, struct interface_config *iface)
991 {
992         unsigned int tmp;
993
994         if (*len < sizeof(iface->hwaddr))
995                 return -1;
996
997         memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
998         *buf += sizeof(iface->hwaddr);
999         *len -= sizeof(iface->hwaddr);
1000
1001         if (read_u32(buf, len, &tmp))
1002                 return -1;
1003         iface->ignore = !!tmp;
1004
1005         if (iface->ignore)
1006                 return 0;
1007
1008         if (read_u32(buf, len, &iface->method))
1009                 return -1;
1010
1011         if (iface->method == CONFIG_METHOD_STATIC) {
1012                 if (read_string(iface, buf, len, &iface->static_config.address))
1013                         return -1;
1014
1015                 if (read_string(iface, buf, len, &iface->static_config.gateway))
1016                         return -1;
1017
1018                 if (read_string(iface, buf, len, &iface->static_config.url))
1019                         return -1;
1020         }
1021
1022         return 0;
1023 }
1024
1025 int pb_protocol_deserialise_config(struct config *config,
1026                 const struct pb_protocol_message *message)
1027 {
1028         unsigned int len, i, tmp;
1029         const char *pos;
1030         int rc = -1;
1031         char *str;
1032
1033         len = message->payload_len;
1034         pos = message->payload;
1035
1036         if (read_u32(&pos, &len, &tmp))
1037                 goto out;
1038         config->autoboot_enabled = !!tmp;
1039
1040         if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
1041                 goto out;
1042
1043         if (read_u32(&pos, &len, &tmp))
1044                 goto out;
1045         config->safe_mode = !!tmp;
1046
1047         if (read_u32(&pos, &len, &config->network.n_interfaces))
1048                 goto out;
1049
1050         config->network.interfaces = talloc_array(config,
1051                         struct interface_config *, config->network.n_interfaces);
1052
1053         for (i = 0; i < config->network.n_interfaces; i++) {
1054                 struct interface_config *iface = talloc_zero(
1055                                 config->network.interfaces,
1056                                 struct interface_config);
1057                 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
1058                         goto out;
1059                 config->network.interfaces[i] = iface;
1060         }
1061
1062         if (read_u32(&pos, &len, &config->network.n_dns_servers))
1063                 goto out;
1064         config->network.dns_servers = talloc_array(config, const char *,
1065                         config->network.n_dns_servers);
1066
1067         for (i = 0; i < config->network.n_dns_servers; i++) {
1068                 if (read_string(config->network.dns_servers, &pos, &len, &str))
1069                         goto out;
1070                 config->network.dns_servers[i] = str;
1071         }
1072
1073         if (read_u32(&pos, &len, &config->n_autoboot_opts))
1074                 goto out;
1075         config->autoboot_opts = talloc_array(config, struct autoboot_option,
1076                         config->n_autoboot_opts);
1077
1078         for (i = 0; i < config->n_autoboot_opts; i++) {
1079                 if (read_u32(&pos, &len, &tmp))
1080                         goto out;
1081                 config->autoboot_opts[i].boot_type = (int)tmp;
1082                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
1083                         if (read_u32(&pos, &len, &tmp))
1084                                 goto out;
1085                         config->autoboot_opts[i].type = tmp;
1086                 } else {
1087                         if (read_string(config, &pos, &len, &str))
1088                                 goto out;
1089                         config->autoboot_opts[i].uuid = str;
1090                 }
1091         }
1092
1093         if (read_u32(&pos, &len, &config->ipmi_bootdev))
1094                 goto out;
1095         if (read_u32(&pos, &len, &tmp))
1096                 goto out;
1097         config->ipmi_bootdev_persistent = !!tmp;
1098
1099         if (read_u32(&pos, &len, &tmp))
1100                 goto out;
1101         config->allow_writes = !!tmp;
1102
1103         if (read_u32(&pos, &len, &config->n_tty))
1104                 goto out;
1105
1106         config->tty_list = talloc_array(config, char *, config->n_tty);
1107         for (i = 0; i < config->n_tty; i++) {
1108                 if (read_string(config->tty_list, &pos, &len, &str))
1109                         goto out;
1110                 config->tty_list[i] = str;
1111         }
1112
1113         if (read_string(config, &pos, &len, &str))
1114                 goto out;
1115
1116         config->boot_tty = str;
1117
1118         if (read_string(config, &pos, &len, &str))
1119                 goto out;
1120
1121         config->lang = str;
1122
1123         rc = 0;
1124
1125 out:
1126         return rc;
1127 }