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