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