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