ui/ncurses: Keep track of the default boot option
[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->is_autoboot_default) +
208                 sizeof(opt->type);
209 }
210
211 int pb_protocol_boot_len(const struct boot_command *boot)
212 {
213         return  4 + optional_strlen(boot->option_id) +
214                 4 + optional_strlen(boot->boot_image_file) +
215                 4 + optional_strlen(boot->initrd_file) +
216                 4 + optional_strlen(boot->dtb_file) +
217                 4 + optional_strlen(boot->boot_args) +
218                 4 + optional_strlen(boot->args_sig_file) +
219                 4 + optional_strlen(boot->console);
220 }
221
222 int pb_protocol_boot_status_len(const struct status *status)
223 {
224         return  4 +     /* type */
225                 4 + optional_strlen(status->message) +
226                 4 +     /* backlog */
227                 4;      /* boot_active */
228 }
229
230 int pb_protocol_system_info_len(const struct system_info *sysinfo)
231 {
232         unsigned int len, i;
233
234         len =   4 + optional_strlen(sysinfo->type) +
235                 4 + optional_strlen(sysinfo->identifier) +
236                 4 + 4;
237
238         len +=  4;
239         for (i = 0; i < sysinfo->n_primary; i++)
240                 len += 4 + optional_strlen(sysinfo->platform_primary[i]);
241         len +=  4;
242         for (i = 0; i < sysinfo->n_other; i++)
243                 len += 4 + optional_strlen(sysinfo->platform_other[i]);
244
245         len +=  4;
246         for (i = 0; i < sysinfo->n_bmc_current; i++)
247                 len += 4 + optional_strlen(sysinfo->bmc_current[i]);
248         len +=  4;
249         for (i = 0; i < sysinfo->n_bmc_golden; i++)
250                 len += 4 + optional_strlen(sysinfo->bmc_golden[i]);
251
252         for (i = 0; i < sysinfo->n_interfaces; i++) {
253                 struct interface_info *if_info = sysinfo->interfaces[i];
254                 len +=  4 + if_info->hwaddr_size +
255                         4 + optional_strlen(if_info->name) +
256                         sizeof(if_info->link) +
257                         4 + optional_strlen(if_info->address) +
258                         4 + optional_strlen(if_info->address_v6);
259         }
260
261         for (i = 0; i < sysinfo->n_blockdevs; i++) {
262                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
263                 len +=  4 + optional_strlen(bd_info->name) +
264                         4 + optional_strlen(bd_info->uuid) +
265                         4 + optional_strlen(bd_info->mountpoint);
266         }
267
268         /* BMC MAC */
269         len += HWADDR_SIZE;
270
271         return len;
272 }
273
274 static int pb_protocol_interface_config_len(struct interface_config *conf)
275 {
276         unsigned int len;
277
278         len =   sizeof(conf->hwaddr) +
279                 4 /* conf->ignore */;
280
281         if (conf->ignore)
282                 return len;
283
284         len += 4 /* conf->method */;
285
286         if (conf->method == CONFIG_METHOD_STATIC) {
287                 len += 4 + optional_strlen(conf->static_config.address);
288                 len += 4 + optional_strlen(conf->static_config.gateway);
289                 len += 4 + optional_strlen(conf->static_config.url);
290         }
291
292         len += 4 /* conf->override */;
293
294         return len;
295 }
296
297 int pb_protocol_config_len(const struct config *config)
298 {
299         unsigned int i, len;
300
301         len =   4 /* config->autoboot_enabled */ +
302                 4 /* config->autoboot_timeout_sec */ +
303                 4 /* config->safe_mode */;
304
305         len += 4;
306         for (i = 0; i < config->network.n_interfaces; i++)
307                 len += pb_protocol_interface_config_len(
308                                 config->network.interfaces[i]);
309
310         len += 4;
311         for (i = 0; i < config->network.n_dns_servers; i++)
312                 len += 4 + optional_strlen(config->network.dns_servers[i]);
313
314         len += 4 + optional_strlen(config->http_proxy);
315         len += 4 + optional_strlen(config->https_proxy);
316
317         len += 4;
318         for (i = 0; i < config->n_autoboot_opts; i++) {
319                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
320                         len += 4 + 4;
321                 else
322                         len += 4 + 4 +
323                                 optional_strlen(config->autoboot_opts[i].uuid);
324         }
325
326         len += 4 + 4; /* ipmi_bootdev, ipmi_bootdev_persistent */
327
328         len += 4; /* allow_writes */
329
330         len += 4; /* n_consoles */
331         for (i = 0; i < config->n_consoles; i++)
332                 len += 4 + optional_strlen(config->consoles[i]);
333
334         len += 4 + optional_strlen(config->boot_console);
335         len += 4; /* manual_console */
336
337         len += 4 + optional_strlen(config->lang);
338
339         return len;
340 }
341
342 int pb_protocol_url_len(const char *url)
343 {
344         /* url + length field */
345         return 4 + optional_strlen(url);
346 }
347
348
349 int pb_protocol_plugin_option_len(const struct plugin_option *opt)
350 {
351         unsigned int i, len = 0;
352
353         len += 4 + optional_strlen(opt->id);
354         len += 4 + optional_strlen(opt->name);
355         len += 4 + optional_strlen(opt->vendor);
356         len += 4 + optional_strlen(opt->vendor_id);
357         len += 4 + optional_strlen(opt->version);
358         len += 4 + optional_strlen(opt->date);
359         len += 4 + optional_strlen(opt->plugin_file);
360
361         len += 4; /* n_executables */
362         for (i = 0; i < opt->n_executables; i++)
363                 len += 4 + optional_strlen(opt->executables[i]);
364
365         return len;
366 }
367
368 int pb_protocol_temp_autoboot_len(const struct autoboot_option *opt)
369 {
370         unsigned int len = 0;
371
372         /* boot_type */
373         len += 4;
374
375         if (opt->boot_type == BOOT_DEVICE_TYPE)
376                 len += 4;
377         else
378                 len += optional_strlen(opt->uuid);
379
380         return len;
381 }
382
383 int pb_protocol_authenticate_len(struct auth_message *msg)
384 {
385         switch (msg->op) {
386         case AUTH_MSG_REQUEST:
387                 /* enum + password + length */
388                 return 4 + 4 + optional_strlen(msg->password);
389         case AUTH_MSG_RESPONSE:
390                 /* enum + bool */
391                 return 4 + 4;
392         case AUTH_MSG_SET:
393                 /* enum + password + password */
394                 return 4 + 4 + optional_strlen(msg->set_password.password) +
395                         4 + optional_strlen(msg->set_password.new_password);
396         default:
397                 pb_log("%s: invalid input\n", __func__);
398                 return 0;
399         }
400 }
401
402 int pb_protocol_serialise_device(const struct device *dev,
403                 char *buf, int buf_len)
404 {
405         char *pos = buf;
406
407         pos += pb_protocol_serialise_string(pos, dev->id);
408         *(enum device_type *)pos = dev->type;
409         pos += sizeof(enum device_type);
410         pos += pb_protocol_serialise_string(pos, dev->name);
411         pos += pb_protocol_serialise_string(pos, dev->description);
412         pos += pb_protocol_serialise_string(pos, dev->icon_file);
413
414         assert(pos <= buf + buf_len);
415         (void)buf_len;
416
417         return 0;
418 }
419
420 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
421                 char *buf, int buf_len)
422 {
423         char *pos = buf;
424
425         pos += pb_protocol_serialise_string(pos, opt->device_id);
426         pos += pb_protocol_serialise_string(pos, opt->id);
427         pos += pb_protocol_serialise_string(pos, opt->name);
428         pos += pb_protocol_serialise_string(pos, opt->description);
429         pos += pb_protocol_serialise_string(pos, opt->icon_file);
430         pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
431         pos += pb_protocol_serialise_string(pos, opt->initrd_file);
432         pos += pb_protocol_serialise_string(pos, opt->dtb_file);
433         pos += pb_protocol_serialise_string(pos, opt->boot_args);
434         pos += pb_protocol_serialise_string(pos, opt->args_sig_file);
435
436         *(bool *)pos = opt->is_default;
437         pos += sizeof(bool);
438         *(bool *)pos = opt->is_autoboot_default;
439         pos += sizeof(bool);
440
441         *(uint32_t *)pos = __cpu_to_be32(opt->type);
442         pos += 4;
443
444         assert(pos <= buf + buf_len);
445         (void)buf_len;
446
447         return 0;
448 }
449
450 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
451                 char *buf, int buf_len)
452 {
453         char *pos = buf;
454
455         pos += pb_protocol_serialise_string(pos, boot->option_id);
456         pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
457         pos += pb_protocol_serialise_string(pos, boot->initrd_file);
458         pos += pb_protocol_serialise_string(pos, boot->dtb_file);
459         pos += pb_protocol_serialise_string(pos, boot->boot_args);
460         pos += pb_protocol_serialise_string(pos, boot->args_sig_file);
461         pos += pb_protocol_serialise_string(pos, boot->console);
462
463         assert(pos <= buf + buf_len);
464         (void)buf_len;
465
466         return 0;
467 }
468
469 int pb_protocol_serialise_boot_status(const struct status *status,
470                 char *buf, int buf_len)
471 {
472         char *pos = buf;
473
474         *(uint32_t *)pos = __cpu_to_be32(status->type);
475         pos += sizeof(uint32_t);
476
477         pos += pb_protocol_serialise_string(pos, status->message);
478
479         *(bool *)pos = __cpu_to_be32(status->backlog);
480         pos += sizeof(bool);
481
482         *(bool *)pos = __cpu_to_be32(status->boot_active);
483         pos += sizeof(bool);
484
485         assert(pos <= buf + buf_len);
486         (void)buf_len;
487
488         return 0;
489 }
490
491 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
492                 char *buf, int buf_len)
493 {
494         char *pos = buf;
495         unsigned int i;
496
497         pos += pb_protocol_serialise_string(pos, sysinfo->type);
498         pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
499
500         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_primary);
501         pos += sizeof(uint32_t);
502         for (i = 0; i < sysinfo->n_primary; i++)
503                 pos += pb_protocol_serialise_string(pos, sysinfo->platform_primary[i]);
504
505         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_other);
506         pos += sizeof(uint32_t);
507         for (i = 0; i < sysinfo->n_other; i++)
508                 pos += pb_protocol_serialise_string(pos, sysinfo->platform_other[i]);
509
510         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_current);
511         pos += sizeof(uint32_t);
512         for (i = 0; i < sysinfo->n_bmc_current; i++)
513                 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_current[i]);
514
515         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_golden);
516         pos += sizeof(uint32_t);
517         for (i = 0; i < sysinfo->n_bmc_golden; i++)
518                 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_golden[i]);
519
520         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
521         pos += sizeof(uint32_t);
522
523         for (i = 0; i < sysinfo->n_interfaces; i++) {
524                 struct interface_info *if_info = sysinfo->interfaces[i];
525
526                 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
527                 pos += sizeof(uint32_t);
528
529                 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
530                 pos += if_info->hwaddr_size;
531
532                 pos += pb_protocol_serialise_string(pos, if_info->name);
533
534                 *(bool *)pos = if_info->link;
535                 pos += sizeof(bool);
536
537                 pos += pb_protocol_serialise_string(pos, if_info->address);
538                 pos += pb_protocol_serialise_string(pos, if_info->address_v6);
539         }
540
541         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
542         pos += sizeof(uint32_t);
543
544         for (i = 0; i < sysinfo->n_blockdevs; i++) {
545                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
546
547                 pos += pb_protocol_serialise_string(pos, bd_info->name);
548                 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
549                 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
550         }
551
552         if (sysinfo->bmc_mac)
553                 memcpy(pos, sysinfo->bmc_mac, HWADDR_SIZE);
554         else
555                 memset(pos, 0, HWADDR_SIZE);
556         pos += HWADDR_SIZE;
557
558         assert(pos <= buf + buf_len);
559         (void)buf_len;
560
561         return 0;
562 }
563
564 static int pb_protocol_serialise_config_interface(char *buf,
565                 struct interface_config *conf)
566 {
567         char *pos = buf;
568
569         memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
570         pos += sizeof(conf->hwaddr);
571
572         *(uint32_t *)pos = conf->ignore;
573         pos += 4;
574
575         if (conf->ignore)
576                 return pos - buf;
577
578         *(uint32_t *)pos = __cpu_to_be32(conf->method);
579         pos += 4;
580
581         if (conf->method == CONFIG_METHOD_STATIC) {
582                 pos += pb_protocol_serialise_string(pos,
583                                 conf->static_config.address);
584                 pos += pb_protocol_serialise_string(pos,
585                                 conf->static_config.gateway);
586                 pos += pb_protocol_serialise_string(pos,
587                                 conf->static_config.url);
588         }
589
590         *(uint32_t *)pos = conf->override;
591         pos += 4;
592
593         return pos - buf;
594 }
595
596 int pb_protocol_serialise_config(const struct config *config,
597                 char *buf, int buf_len)
598 {
599         char *pos = buf;
600         unsigned int i;
601
602         *(uint32_t *)pos = config->autoboot_enabled;
603         pos += 4;
604
605         *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
606         pos += 4;
607
608         *(uint32_t *)pos = config->safe_mode;
609         pos += 4;
610
611         *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
612         pos += 4;
613         for (i = 0; i < config->network.n_interfaces; i++) {
614                 struct interface_config *iface =
615                         config->network.interfaces[i];
616                 pos += pb_protocol_serialise_config_interface(pos, iface);
617         }
618
619         *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
620         pos += 4;
621         for (i = 0; i < config->network.n_dns_servers; i++) {
622                 pos += pb_protocol_serialise_string(pos,
623                                 config->network.dns_servers[i]);
624         }
625
626         pos += pb_protocol_serialise_string(pos, config->http_proxy);
627         pos += pb_protocol_serialise_string(pos, config->https_proxy);
628
629         *(uint32_t *)pos = __cpu_to_be32(config->n_autoboot_opts);
630         pos += 4;
631         for (i = 0; i < config->n_autoboot_opts; i++) {
632                 *(uint32_t *)pos =
633                         __cpu_to_be32(config->autoboot_opts[i].boot_type);
634                 pos += 4;
635                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
636                         *(uint32_t *)pos =
637                                 __cpu_to_be32(config->autoboot_opts[i].type);
638                         pos += 4;
639                 } else {
640                         pos += pb_protocol_serialise_string(pos,
641                                                 config->autoboot_opts[i].uuid);
642                 }
643         }
644
645         *(uint32_t *)pos = __cpu_to_be32(config->ipmi_bootdev);
646         pos += 4;
647         *(uint32_t *)pos = config->ipmi_bootdev_persistent;
648         pos += 4;
649
650         *(uint32_t *)pos = config->allow_writes;
651         pos += 4;
652
653         *(uint32_t *)pos = __cpu_to_be32(config->n_consoles);
654         pos += 4;
655         for (i = 0; i < config->n_consoles; i++)
656                 pos += pb_protocol_serialise_string(pos, config->consoles[i]);
657
658         pos += pb_protocol_serialise_string(pos, config->boot_console);
659         *(uint32_t *)pos = config->manual_console;
660         pos += 4;
661
662         pos += pb_protocol_serialise_string(pos, config->lang);
663
664         assert(pos <= buf + buf_len);
665         (void)buf_len;
666
667         return 0;
668 }
669
670 int pb_protocol_serialise_url(const char *url, char *buf, int buf_len)
671 {
672         char *pos = buf;
673
674         pos += pb_protocol_serialise_string(pos, url);
675
676         assert(pos <=buf+buf_len);
677         (void)buf_len;
678
679         return 0;
680 }
681
682 int pb_protocol_serialise_plugin_option(const struct plugin_option *opt,
683                 char *buf, int buf_len)
684 {
685         char *pos = buf;
686         unsigned int i;
687
688         pos += pb_protocol_serialise_string(pos, opt->id);
689         pos += pb_protocol_serialise_string(pos, opt->name);
690         pos += pb_protocol_serialise_string(pos, opt->vendor);
691         pos += pb_protocol_serialise_string(pos, opt->vendor_id);
692         pos += pb_protocol_serialise_string(pos, opt->version);
693         pos += pb_protocol_serialise_string(pos, opt->date);
694         pos += pb_protocol_serialise_string(pos, opt->plugin_file);
695
696         *(uint32_t *)pos = __cpu_to_be32(opt->n_executables);
697         pos += 4;
698
699         for (i = 0; i < opt->n_executables; i++)
700                 pos += pb_protocol_serialise_string(pos, opt->executables[i]);
701
702         assert(pos <= buf + buf_len);
703         (void)buf_len;
704
705         return 0;
706 }
707
708 int pb_protocol_serialise_temp_autoboot(const struct autoboot_option *opt,
709                 char *buf, int buf_len)
710 {
711         char *pos = buf;
712
713         *(uint32_t *)pos = __cpu_to_be32(opt->boot_type);
714         pos += 4;
715
716         if (opt->boot_type == BOOT_DEVICE_TYPE) {
717                 *(uint32_t *)pos = __cpu_to_be32(opt->type);
718                 pos += 4;
719         } else {
720                 pos += pb_protocol_serialise_string(pos, opt->uuid);
721         }
722
723         (void)buf_len;
724
725         return 0;
726 }
727
728 int pb_protocol_serialise_authenticate(struct auth_message *msg,
729                 char *buf, int buf_len)
730 {
731         char *pos = buf;
732
733         *(enum auth_msg_type *)pos = msg->op;
734         pos += sizeof(enum auth_msg_type);
735
736         switch(msg->op) {
737         case AUTH_MSG_REQUEST:
738                 pos += pb_protocol_serialise_string(pos, msg->password);
739                 break;
740         case AUTH_MSG_RESPONSE:
741                 *(bool *)pos = msg->authenticated;
742                 pos += sizeof(bool);
743                 break;
744         case AUTH_MSG_SET:
745                 pos += pb_protocol_serialise_string(pos,
746                                 msg->set_password.password);
747                 pos += pb_protocol_serialise_string(pos,
748                                 msg->set_password.new_password);
749                 break;
750         default:
751                 pb_log("%s: invalid msg\n", __func__);
752                 return -1;
753         };
754
755         assert(pos <= buf + buf_len);
756         (void)buf_len;
757
758         return 0;
759 }
760
761 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
762 {
763         int total_len, rc;
764         char *pos;
765
766         total_len = sizeof(*message) + message->payload_len;
767
768         message->payload_len = __cpu_to_be32(message->payload_len);
769         message->action = __cpu_to_be32(message->action);
770
771         for (pos = (void *)message; total_len;) {
772                 rc = write(fd, pos, total_len);
773
774                 if (rc <= 0)
775                         break;
776
777                 total_len -= rc;
778                 pos += rc;
779         }
780
781         talloc_free(message);
782
783         if (!total_len)
784                 return 0;
785
786         pb_log_fn("failed: %s\n", strerror(errno));
787         return -1;
788 }
789
790 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
791                 enum pb_protocol_action action, int payload_len)
792 {
793         struct pb_protocol_message *message;
794
795         if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
796                 pb_log_fn("payload too big %u/%u\n", payload_len,
797                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
798                 return NULL;
799         }
800
801         message = talloc_size(ctx, sizeof(*message) + payload_len);
802
803         /* we convert these to big-endian in write_message() */
804         message->action = action;
805         message->payload_len = payload_len;
806
807         return message;
808
809 }
810
811 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
812 {
813         struct pb_protocol_message *message, m;
814         int rc;
815         unsigned int len;
816
817         /* use the stack for the initial 8-byte read */
818
819         rc = read(fd, &m, sizeof(m));
820         if (rc != sizeof(m))
821                 return NULL;
822
823         m.payload_len = __be32_to_cpu(m.payload_len);
824         m.action = __be32_to_cpu(m.action);
825
826         if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
827                 pb_log_fn("payload too big %u/%u\n", m.payload_len,
828                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
829                 return NULL;
830         }
831
832         message = talloc_size(ctx, sizeof(m) + m.payload_len);
833         memcpy(message, &m, sizeof(m));
834
835         for (len = 0; len < m.payload_len;) {
836                 rc = read(fd, message->payload + len, m.payload_len - len);
837
838                 if (rc <= 0) {
839                         talloc_free(message);
840                         pb_log_fn("failed (%u): %s\n", len,
841                                 strerror(errno));
842                         return NULL;
843                 }
844
845                 len += rc;
846         }
847
848         return message;
849 }
850
851
852 int pb_protocol_deserialise_device(struct device *dev,
853                 const struct pb_protocol_message *message)
854 {
855         unsigned int len;
856         const char *pos;
857         int rc = -1;
858
859         len = message->payload_len;
860         pos = message->payload;
861
862         if (read_string(dev, &pos, &len, &dev->id))
863                 goto out;
864
865         if (len < sizeof(enum device_type))
866                 goto out;
867         dev->type = *(enum device_type *)(pos);
868         pos += sizeof(enum device_type);
869         len -= sizeof(enum device_type);
870
871         if (read_string(dev, &pos, &len, &dev->name))
872                 goto out;
873
874         if (read_string(dev, &pos, &len, &dev->description))
875                 goto out;
876
877         if (read_string(dev, &pos, &len, &dev->icon_file))
878                 goto out;
879
880         rc = 0;
881
882 out:
883         return rc;
884 }
885
886 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
887                 const struct pb_protocol_message *message)
888 {
889         unsigned int len;
890         const char *pos;
891         int rc = -1;
892
893         len = message->payload_len;
894         pos = message->payload;
895
896         if (read_string(opt, &pos, &len, &opt->device_id))
897                 goto out;
898
899         if (read_string(opt, &pos, &len, &opt->id))
900                 goto out;
901
902         if (read_string(opt, &pos, &len, &opt->name))
903                 goto out;
904
905         if (read_string(opt, &pos, &len, &opt->description))
906                 goto out;
907
908         if (read_string(opt, &pos, &len, &opt->icon_file))
909                 goto out;
910
911         if (read_string(opt, &pos, &len, &opt->boot_image_file))
912                 goto out;
913
914         if (read_string(opt, &pos, &len, &opt->initrd_file))
915                 goto out;
916
917         if (read_string(opt, &pos, &len, &opt->dtb_file))
918                 goto out;
919
920         if (read_string(opt, &pos, &len, &opt->boot_args))
921                 goto out;
922
923         if (read_string(opt, &pos, &len, &opt->args_sig_file))
924                 goto out;
925
926         if (len < sizeof(bool))
927                 goto out;
928         opt->is_default = *(bool *)(pos);
929         pos += sizeof(bool);
930         len -= sizeof(bool);
931         opt->is_autoboot_default = *(bool *)(pos);
932         pos += sizeof(bool);
933         len -= sizeof(bool);
934
935         if (read_u32(&pos, &len, &opt->type))
936                 return -1;
937
938         rc = 0;
939
940 out:
941         return rc;
942 }
943
944 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
945                 const struct pb_protocol_message *message)
946 {
947         unsigned int len;
948         const char *pos;
949         int rc = -1;
950
951         len = message->payload_len;
952         pos = message->payload;
953
954         if (read_string(cmd, &pos, &len, &cmd->option_id))
955                 goto out;
956
957         if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
958                 goto out;
959
960         if (read_string(cmd, &pos, &len, &cmd->initrd_file))
961                 goto out;
962
963         if (read_string(cmd, &pos, &len, &cmd->dtb_file))
964                 goto out;
965
966         if (read_string(cmd, &pos, &len, &cmd->boot_args))
967                 goto out;
968
969         if (read_string(cmd, &pos, &len, &cmd->args_sig_file))
970                 goto out;
971
972         if (read_string(cmd, &pos, &len, &cmd->console))
973                 goto out;
974
975         rc = 0;
976
977 out:
978         return rc;
979 }
980
981 int pb_protocol_deserialise_boot_status(struct status *status,
982                 const struct pb_protocol_message *message)
983 {
984         unsigned int len;
985         const char *pos;
986         int rc = -1;
987
988         len = message->payload_len;
989         pos = message->payload;
990
991         /* first up, the type enum... */
992         if (len < sizeof(uint32_t))
993                 goto out;
994
995         status->type = __be32_to_cpu(*(uint32_t *)(pos));
996
997         switch (status->type) {
998         case STATUS_ERROR:
999         case STATUS_INFO:
1000                 break;
1001         default:
1002                 goto out;
1003         }
1004
1005         pos += sizeof(uint32_t);
1006         len -= sizeof(uint32_t);
1007
1008         /* message string */
1009         if (read_string(status, &pos, &len, &status->message))
1010                 goto out;
1011
1012         /* backlog */
1013         status->backlog = *(bool *)pos;
1014         pos += sizeof(status->backlog);
1015
1016         /* boot_active */
1017         status->boot_active = *(bool *)pos;
1018         pos += sizeof(status->boot_active);
1019
1020         rc = 0;
1021
1022 out:
1023         return rc;
1024 }
1025
1026 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
1027                 const struct pb_protocol_message *message)
1028 {
1029         unsigned int len, i;
1030         const char *pos;
1031         int rc = -1;
1032         char *tmp;
1033
1034         len = message->payload_len;
1035         pos = message->payload;
1036
1037         /* type and identifier strings */
1038         if (read_string(sysinfo, &pos, &len, &sysinfo->type))
1039                 goto out;
1040
1041         if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
1042                 goto out;
1043
1044         /* Platform version strings for openpower platforms */
1045         if (read_u32(&pos, &len, &sysinfo->n_primary))
1046                 goto out;
1047         sysinfo->platform_primary = talloc_array(sysinfo, char *,
1048                                                 sysinfo->n_primary);
1049         for (i = 0; i < sysinfo->n_primary; i++) {
1050                 if (read_string(sysinfo, &pos, &len, &tmp))
1051                         goto out;
1052                 sysinfo->platform_primary[i] = talloc_strdup(sysinfo, tmp);
1053         }
1054
1055         if (read_u32(&pos, &len, &sysinfo->n_other))
1056                 goto out;
1057         sysinfo->platform_other = talloc_array(sysinfo, char *,
1058                                                 sysinfo->n_other);
1059         for (i = 0; i < sysinfo->n_other; i++) {
1060                 if (read_string(sysinfo, &pos, &len, &tmp))
1061                         goto out;
1062                 sysinfo->platform_other[i] = talloc_strdup(sysinfo, tmp);
1063         }
1064
1065         /* BMC version strings for openpower platforms */
1066         if (read_u32(&pos, &len, &sysinfo->n_bmc_current))
1067                 goto out;
1068         sysinfo->bmc_current = talloc_array(sysinfo, char *,
1069                                                 sysinfo->n_bmc_current);
1070         for (i = 0; i < sysinfo->n_bmc_current; i++) {
1071                 if (read_string(sysinfo, &pos, &len, &tmp))
1072                         goto out;
1073                 sysinfo->bmc_current[i] = talloc_strdup(sysinfo, tmp);
1074         }
1075
1076         if (read_u32(&pos, &len, &sysinfo->n_bmc_golden))
1077                 goto out;
1078         sysinfo->bmc_golden = talloc_array(sysinfo, char *,
1079                                                 sysinfo->n_bmc_golden);
1080         for (i = 0; i < sysinfo->n_bmc_golden; i++) {
1081                 if (read_string(sysinfo, &pos, &len, &tmp))
1082                         goto out;
1083                 sysinfo->bmc_golden[i] = talloc_strdup(sysinfo, tmp);
1084         }
1085
1086         /* number of interfaces */
1087         if (read_u32(&pos, &len, &sysinfo->n_interfaces))
1088                 goto out;
1089
1090         sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
1091                         sysinfo->n_interfaces);
1092
1093         for (i = 0; i < sysinfo->n_interfaces; i++) {
1094                 struct interface_info *if_info = talloc(sysinfo,
1095                                                         struct interface_info);
1096
1097                 if (read_u32(&pos, &len, &if_info->hwaddr_size))
1098                         goto out;
1099
1100                 if (len < if_info->hwaddr_size)
1101                         goto out;
1102
1103                 if_info->hwaddr = talloc_memdup(if_info, pos,
1104                                                 if_info->hwaddr_size);
1105                 pos += if_info->hwaddr_size;
1106                 len -= if_info->hwaddr_size;
1107
1108                 if (read_string(if_info, &pos, &len, &if_info->name))
1109                         goto out;
1110
1111                 if_info->link = *(bool *)pos;
1112                 pos += sizeof(if_info->link);
1113
1114                 if (read_string(if_info, &pos, &len, &if_info->address))
1115                         goto out;
1116                 if (read_string(if_info, &pos, &len, &if_info->address_v6))
1117                         goto out;
1118
1119                 sysinfo->interfaces[i] = if_info;
1120         }
1121
1122         /* number of interfaces */
1123         if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
1124                 goto out;
1125
1126         sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
1127                         sysinfo->n_blockdevs);
1128
1129         for (i = 0; i < sysinfo->n_blockdevs; i++) {
1130                 struct blockdev_info *bd_info = talloc(sysinfo,
1131                                                         struct blockdev_info);
1132
1133                 if (read_string(bd_info, &pos, &len, &bd_info->name))
1134                         goto out;
1135
1136                 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
1137                         goto out;
1138
1139                 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
1140                         goto out;
1141
1142                 sysinfo->blockdevs[i] = bd_info;
1143         }
1144
1145         for (i = 0; i < HWADDR_SIZE; i++) {
1146                 if (pos[i] != 0) {
1147                         sysinfo->bmc_mac = talloc_memdup(sysinfo, pos, HWADDR_SIZE);
1148                         break;
1149                 }
1150         }
1151
1152         pos += HWADDR_SIZE;
1153         len -= HWADDR_SIZE;
1154
1155         rc = 0;
1156 out:
1157         return rc;
1158 }
1159
1160 static int pb_protocol_deserialise_config_interface(const char **buf,
1161                 unsigned int *len, struct interface_config *iface)
1162 {
1163         unsigned int tmp;
1164
1165         if (*len < sizeof(iface->hwaddr))
1166                 return -1;
1167
1168         memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
1169         *buf += sizeof(iface->hwaddr);
1170         *len -= sizeof(iface->hwaddr);
1171
1172         if (read_u32(buf, len, &tmp))
1173                 return -1;
1174         iface->ignore = !!tmp;
1175
1176         if (iface->ignore)
1177                 return 0;
1178
1179         if (read_u32(buf, len, &iface->method))
1180                 return -1;
1181
1182         if (iface->method == CONFIG_METHOD_STATIC) {
1183                 if (read_string(iface, buf, len, &iface->static_config.address))
1184                         return -1;
1185
1186                 if (read_string(iface, buf, len, &iface->static_config.gateway))
1187                         return -1;
1188
1189                 if (read_string(iface, buf, len, &iface->static_config.url))
1190                         return -1;
1191         }
1192
1193         if (read_u32(buf, len, &tmp))
1194                 return -1;
1195         iface->override = !!tmp;
1196
1197         return 0;
1198 }
1199
1200 int pb_protocol_deserialise_config(struct config *config,
1201                 const struct pb_protocol_message *message)
1202 {
1203         unsigned int len, i, tmp;
1204         const char *pos;
1205         int rc = -1;
1206         char *str;
1207
1208         len = message->payload_len;
1209         pos = message->payload;
1210
1211         if (read_u32(&pos, &len, &tmp))
1212                 goto out;
1213         config->autoboot_enabled = !!tmp;
1214
1215         if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
1216                 goto out;
1217
1218         if (read_u32(&pos, &len, &tmp))
1219                 goto out;
1220         config->safe_mode = !!tmp;
1221
1222         if (read_u32(&pos, &len, &config->network.n_interfaces))
1223                 goto out;
1224
1225         config->network.interfaces = talloc_array(config,
1226                         struct interface_config *, config->network.n_interfaces);
1227
1228         for (i = 0; i < config->network.n_interfaces; i++) {
1229                 struct interface_config *iface = talloc_zero(
1230                                 config->network.interfaces,
1231                                 struct interface_config);
1232                 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
1233                         goto out;
1234                 config->network.interfaces[i] = iface;
1235         }
1236
1237         if (read_u32(&pos, &len, &config->network.n_dns_servers))
1238                 goto out;
1239         config->network.dns_servers = talloc_array(config, const char *,
1240                         config->network.n_dns_servers);
1241
1242         for (i = 0; i < config->network.n_dns_servers; i++) {
1243                 if (read_string(config->network.dns_servers, &pos, &len, &str))
1244                         goto out;
1245                 config->network.dns_servers[i] = str;
1246         }
1247
1248         if (read_string(config, &pos, &len, &str))
1249                 goto out;
1250         config->http_proxy = str;
1251         if (read_string(config, &pos, &len, &str))
1252                 goto out;
1253         config->https_proxy = str;
1254
1255         if (read_u32(&pos, &len, &config->n_autoboot_opts))
1256                 goto out;
1257         config->autoboot_opts = talloc_array(config, struct autoboot_option,
1258                         config->n_autoboot_opts);
1259
1260         for (i = 0; i < config->n_autoboot_opts; i++) {
1261                 if (read_u32(&pos, &len, &tmp))
1262                         goto out;
1263                 config->autoboot_opts[i].boot_type = (int)tmp;
1264                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
1265                         if (read_u32(&pos, &len, &tmp))
1266                                 goto out;
1267                         config->autoboot_opts[i].type = tmp;
1268                 } else {
1269                         if (read_string(config, &pos, &len, &str))
1270                                 goto out;
1271                         config->autoboot_opts[i].uuid = str;
1272                 }
1273         }
1274
1275         if (read_u32(&pos, &len, &config->ipmi_bootdev))
1276                 goto out;
1277         if (read_u32(&pos, &len, &tmp))
1278                 goto out;
1279         config->ipmi_bootdev_persistent = !!tmp;
1280
1281         if (read_u32(&pos, &len, &tmp))
1282                 goto out;
1283         config->allow_writes = !!tmp;
1284
1285         if (read_u32(&pos, &len, &config->n_consoles))
1286                 goto out;
1287
1288         config->consoles = talloc_array(config, char *, config->n_consoles);
1289         for (i = 0; i < config->n_consoles; i++) {
1290                 if (read_string(config->consoles, &pos, &len, &str))
1291                         goto out;
1292                 config->consoles[i] = str;
1293         }
1294
1295         if (read_string(config, &pos, &len, &str))
1296                 goto out;
1297
1298         config->boot_console = str;
1299
1300         if (read_u32(&pos, &len, &tmp))
1301                 goto out;
1302         config->manual_console = !!tmp;
1303
1304         if (read_string(config, &pos, &len, &str))
1305                 goto out;
1306
1307         config->lang = str;
1308
1309         rc = 0;
1310
1311 out:
1312         return rc;
1313 }
1314
1315 int pb_protocol_deserialise_plugin_option(struct plugin_option *opt,
1316                 const struct pb_protocol_message *message)
1317 {
1318         unsigned int len, i, tmp;
1319         const char *pos;
1320         int rc = -1;
1321         char *str;
1322
1323         len = message->payload_len;
1324         pos = message->payload;
1325
1326         if (read_string(opt, &pos, &len, &str))
1327                 goto out;
1328         opt->id = str;
1329
1330         if (read_string(opt, &pos, &len, &str))
1331                 goto out;
1332         opt->name = str;
1333
1334         if (read_string(opt, &pos, &len, &str))
1335                 goto out;
1336         opt->vendor = str;
1337
1338         if (read_string(opt, &pos, &len, &str))
1339                 goto out;
1340         opt->vendor_id = str;
1341
1342         if (read_string(opt, &pos, &len, &str))
1343                 goto out;
1344         opt->version = str;
1345
1346         if (read_string(opt, &pos, &len, &str))
1347                 goto out;
1348         opt->date = str;
1349
1350         if (read_string(opt, &pos, &len, &str))
1351                 goto out;
1352         opt->plugin_file = str;
1353
1354         if (read_u32(&pos, &len, &tmp))
1355                 goto out;
1356         opt->n_executables = tmp;
1357
1358         opt->executables = talloc_zero_array(opt, char *, opt->n_executables);
1359         if (!opt->executables)
1360                 goto out;
1361
1362         for (i = 0; i < opt->n_executables; i++) {
1363                 if (read_string(opt, &pos, &len, &str))
1364                         goto out;
1365                 opt->executables[i] = talloc_strdup(opt, str);
1366         }
1367
1368         rc = 0;
1369 out:
1370         return rc;
1371 }
1372
1373 int pb_protocol_deserialise_temp_autoboot(struct autoboot_option *opt,
1374                 const struct pb_protocol_message *message)
1375 {
1376         unsigned int len, tmp;
1377         const char *pos;
1378         int rc = -1;
1379         char *str;
1380
1381         len = message->payload_len;
1382         pos = message->payload;
1383
1384         if (read_u32(&pos, &len, &tmp))
1385                 goto out;
1386
1387         opt->boot_type = tmp;
1388         if (opt->boot_type == BOOT_DEVICE_TYPE) {
1389                 if (read_u32(&pos, &len, &tmp))
1390                         goto out;
1391                 opt->type = tmp;
1392
1393         } else if (opt->boot_type == BOOT_DEVICE_UUID) {
1394                 if (read_string(opt, &pos, &len, &str))
1395                         goto out;
1396                 opt->uuid = str;
1397
1398         } else {
1399                 return -1;
1400         }
1401
1402         rc = 0;
1403
1404 out:
1405         return rc;
1406 }
1407
1408 int pb_protocol_deserialise_authenticate(struct auth_message *msg,
1409                 const struct pb_protocol_message *message)
1410 {
1411         unsigned int len;
1412         const char *pos;
1413
1414         len = message->payload_len;
1415         pos = message->payload;
1416
1417         msg->op = *(enum auth_msg_type *)pos;
1418         pos += sizeof(enum auth_msg_type);
1419
1420         switch (msg->op) {
1421         case AUTH_MSG_REQUEST:
1422                 if (read_string(msg, &pos, &len, &msg->password))
1423                         return -1;
1424                 break;
1425         case AUTH_MSG_RESPONSE:
1426                 msg->authenticated = *(bool *)pos;
1427                 pos += sizeof(bool);
1428                 break;
1429         case AUTH_MSG_SET:
1430                 if (read_string(msg, &pos, &len, &msg->set_password.password))
1431                         return -1;
1432                 if (read_string(msg, &pos, &len,
1433                                         &msg->set_password.new_password))
1434                         return -1;
1435                 break;
1436         default:
1437                 pb_log("%s: unable to parse\n", __func__);
1438                 return -1;
1439         }
1440
1441         return 0;
1442 }