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