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