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