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