2 * Copyright (c) 2013 Baptiste Daroussin <bapt@FreeBSD.org>
3 * Copyright (c) 2013 Bryan Drewery <bdrewery@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/elf_common.h>
34 #include <sys/endian.h>
35 #include <sys/types.h>
51 #include "elf_tables.h"
54 #define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
58 STAILQ_ENTRY(config_value) next;
66 STAILQ_HEAD(, config_value) *list;
70 static struct config_entry c[] = {
74 URL_SCHEME_PREFIX "http://pkg.FreeBSD.org/${ABI}/latest",
95 [ASSUME_ALWAYS_YES] = {
130 elf_corres_to_string(struct _elf_corres *m, int e)
134 for (i = 0; m[i].string != NULL; i++)
135 if (m[i].elf_nb == e)
136 return (m[i].string);
142 aeabi_parse_arm_attributes(void *data, size_t length)
145 uint8_t *section = data;
147 #define MOVE(len) do { \
148 assert(length >= (len)); \
153 if (length == 0 || *section != 'A')
158 /* Read the section length */
159 if (length < sizeof(sect_len))
162 memcpy(§_len, section, sizeof(sect_len));
165 * The section length should be no longer than the section it is within
167 if (sect_len > length)
170 MOVE(sizeof(sect_len));
172 /* Skip the vendor name */
173 while (length != 0) {
174 if (*section == '\0')
182 while (length != 0) {
186 case 1: /* Tag_File */
188 if (length < sizeof(tag_length))
190 memcpy(&tag_length, section, sizeof(tag_length));
192 case 2: /* Tag_Section */
193 case 3: /* Tag_Symbol */
197 /* At least space for the tag and size */
201 /* Check the tag fits */
202 if (tag_length > length)
205 #define MOVE_TAG(len) do { \
206 assert(tag_length >= (len)); \
208 tag_length -= (len); \
211 MOVE(sizeof(tag_length));
212 tag_length -= sizeof(tag_length);
214 while (tag_length != 0) {
217 assert(tag_length >= length);
223 * These tag values come from:
225 * Addenda to, and Errata in, the ABI for the
226 * ARM Architecture. Release 2.08, section 2.3.
228 if (tag == 6) { /* == Tag_CPU_arch */
233 * We don't support values that require
234 * more than one byte.
239 /* We have an ARMv4 or ARMv5 */
242 else /* We have an ARMv6+ */
244 } else if (tag == 4 || tag == 5 || tag == 32 ||
245 tag == 65 || tag == 67) {
246 while (*section != '\0' && length != 0)
250 /* Skip the last byte */
252 } else if ((tag >= 7 && tag <= 31) || tag == 34 ||
253 tag == 36 || tag == 38 || tag == 42 || tag == 44 ||
254 tag == 64 || tag == 66 || tag == 68 || tag == 70) {
255 /* Skip the uleb128 data */
256 while (*section & (1 << 7) && length != 0)
260 /* Skip the last byte */
274 pkg_get_myabi(char *dest, size_t sz)
281 const char *arch, *abi, *fpu, *endian_corres_str;
282 const char *wordsize_corres_str;
293 if (elf_version(EV_CURRENT) == EV_NONE) {
294 warnx("ELF library initialization failed: %s",
299 if ((fd = open(_PATH_BSHELL, O_RDONLY)) < 0) {
304 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
306 warnx("elf_begin() failed: %s.", elf_errmsg(-1));
310 if (gelf_getehdr(elf, &elfhdr) == NULL) {
312 warn("getehdr() failed: %s.", elf_errmsg(-1));
315 while ((scn = elf_nextscn(elf, scn)) != NULL) {
316 if (gelf_getshdr(scn, &shdr) != &shdr) {
318 warn("getshdr() failed: %s.", elf_errmsg(-1));
322 if (shdr.sh_type == SHT_NOTE)
328 warn("failed to get the note section");
332 data = elf_getdata(scn, NULL);
335 memcpy(¬e, src, sizeof(Elf_Note));
336 src += sizeof(Elf_Note);
337 if (note.n_type == NT_VERSION)
339 src += note.n_namesz + note.n_descsz;
342 src += roundup2(note.n_namesz, 4);
343 if (elfhdr.e_ident[EI_DATA] == ELFDATA2MSB)
344 version = be32dec(src);
346 version = le32dec(src);
348 for (i = 0; osname[i] != '\0'; i++)
349 osname[i] = (char)tolower(osname[i]);
351 wordsize_corres_str = elf_corres_to_string(wordsize_corres,
352 (int)elfhdr.e_ident[EI_CLASS]);
354 arch = elf_corres_to_string(mach_corres, (int) elfhdr.e_machine);
356 snprintf(dest, sz, "%s:%d",
357 osname, version / 100000);
361 switch (elfhdr.e_machine) {
363 endian_corres_str = elf_corres_to_string(endian_corres,
364 (int)elfhdr.e_ident[EI_DATA]);
366 /* FreeBSD doesn't support the hard-float ABI yet */
368 if ((elfhdr.e_flags & 0xFF000000) != 0) {
369 const char *sh_name = NULL;
372 /* This is an EABI file, the conformance level is set */
374 /* Find which TARGET_ARCH we are building for. */
375 elf_getshdrstrndx(elf, &shstrndx);
376 while ((scn = elf_nextscn(elf, scn)) != NULL) {
378 if (gelf_getshdr(scn, &shdr) != &shdr) {
383 sh_name = elf_strptr(elf, shstrndx,
387 if (strcmp(".ARM.attributes", sh_name) == 0)
390 if (scn != NULL && sh_name != NULL) {
391 data = elf_getdata(scn, NULL);
393 * Prior to FreeBSD 10.0 libelf would return
394 * NULL from elf_getdata on the .ARM.attributes
395 * section. As this was the first release to
396 * get armv6 support assume a NULL value means
399 * This assumption can be removed when 9.x
403 arch = aeabi_parse_arm_attributes(
404 data->d_buf, data->d_size);
407 warn("unknown ARM ARCH");
413 warn("Unable to find the .ARM.attributes "
417 } else if (elfhdr.e_ident[EI_OSABI] != ELFOSABI_NONE) {
419 * EABI executables all have this field set to
420 * ELFOSABI_NONE, therefore it must be an oabi file.
425 warn("unknown ARM ABI");
428 snprintf(dest + strlen(dest), sz - strlen(dest),
429 ":%s:%s:%s:%s:%s", arch, wordsize_corres_str,
430 endian_corres_str, abi, fpu);
434 * this is taken from binutils sources:
436 * mapping is figured out from binutils:
437 * gas/config/tc-mips.c
439 switch (elfhdr.e_flags & EF_MIPS_ABI) {
447 if (elfhdr.e_ident[EI_DATA] ==
450 else if (elfhdr.e_ident[EI_DATA] ==
455 endian_corres_str = elf_corres_to_string(endian_corres,
456 (int)elfhdr.e_ident[EI_DATA]);
458 snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s:%s:%s",
459 arch, wordsize_corres_str, endian_corres_str, abi);
462 snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s",
463 arch, wordsize_corres_str);
475 subst_packagesite(const char *abi)
478 const char *variable_string;
481 if (c[PACKAGESITE].value != NULL)
482 oldval = c[PACKAGESITE].value;
484 oldval = c[PACKAGESITE].val;
486 if ((variable_string = strstr(oldval, "${ABI}")) == NULL)
489 newval = sbuf_new_auto();
490 sbuf_bcat(newval, oldval, variable_string - oldval);
491 sbuf_cat(newval, abi);
492 sbuf_cat(newval, variable_string + strlen("${ABI}"));
495 free(c[PACKAGESITE].value);
496 c[PACKAGESITE].value = strdup(sbuf_data(newval));
500 boolstr_to_bool(const char *str)
502 if (str != NULL && (strcasecmp(str, "true") == 0 ||
503 strcasecmp(str, "yes") == 0 || strcasecmp(str, "on") == 0 ||
511 config_parse(yaml_document_t *doc, yaml_node_t *node, pkg_conf_file_t conftype)
513 yaml_node_item_t *item;
514 yaml_node_pair_t *pair;
515 yaml_node_t *key, *val, *item_val;
516 struct sbuf *buf = sbuf_new_auto();
517 struct config_entry *temp_config;
518 struct config_value *cv;
522 pair = node->data.mapping.pairs.start;
524 /* Temporary config for configs that may be disabled. */
525 temp_config = calloc(CONFIG_SIZE, sizeof(struct config_entry));
527 while (pair < node->data.mapping.pairs.top) {
528 key = yaml_document_get_node(doc, pair->key);
529 val = yaml_document_get_node(doc, pair->value);
532 * ignoring silently empty keys can be empty lines
535 if (key->data.scalar.length <= 0) {
541 * silently skip on purpose to allow user to leave
542 * empty lines without complaining
544 if (val->type == YAML_NO_NODE ||
545 (val->type == YAML_SCALAR_NODE &&
546 val->data.scalar.length <= 0)) {
553 if (conftype == CONFFILE_PKG) {
554 for (j = 0; j < strlen(key->data.scalar.value); ++j)
556 toupper(key->data.scalar.value[j]));
558 } else if (conftype == CONFFILE_REPO) {
559 /* The CONFFILE_REPO type is more restrictive. Only
560 parse known elements. */
561 if (strcasecmp(key->data.scalar.value, "url") == 0)
562 sbuf_cpy(buf, "PACKAGESITE");
563 else if (strcasecmp(key->data.scalar.value,
565 sbuf_cpy(buf, "MIRROR_TYPE");
566 else if (strcasecmp(key->data.scalar.value,
567 "signature_type") == 0)
568 sbuf_cpy(buf, "SIGNATURE_TYPE");
569 else if (strcasecmp(key->data.scalar.value,
570 "fingerprints") == 0)
571 sbuf_cpy(buf, "FINGERPRINTS");
572 else if (strcasecmp(key->data.scalar.value,
574 /* Skip disabled repos. */
575 if (!boolstr_to_bool(val->data.scalar.value))
577 } else { /* Skip unknown entries for future use. */
584 for (i = 0; i < CONFIG_SIZE; i++) {
585 if (strcmp(sbuf_data(buf), c[i].key) == 0)
589 /* Silently skip unknown keys to be future compatible. */
590 if (i == CONFIG_SIZE) {
595 /* env has priority over config file */
601 /* Parse sequence value ["item1", "item2"] */
603 case PKG_CONFIG_LIST:
604 if (val->type != YAML_SEQUENCE_NODE) {
605 fprintf(stderr, "Skipping invalid array "
606 "value for %s.\n", c[i].key);
610 item = val->data.sequence.items.start;
611 temp_config[i].list =
612 malloc(sizeof(*temp_config[i].list));
613 STAILQ_INIT(temp_config[i].list);
615 while (item < val->data.sequence.items.top) {
616 item_val = yaml_document_get_node(doc, *item);
617 if (item_val->type != YAML_SCALAR_NODE) {
621 cv = malloc(sizeof(struct config_value));
623 strdup(item_val->data.scalar.value);
624 STAILQ_INSERT_TAIL(temp_config[i].list, cv,
630 /* Normal string value. */
631 temp_config[i].value = strdup(val->data.scalar.value);
637 /* Repo is enabled, copy over all settings from temp_config. */
638 for (i = 0; i < CONFIG_SIZE; i++) {
642 case PKG_CONFIG_LIST:
643 c[i].list = temp_config[i].list;
646 c[i].value = temp_config[i].value;
657 * Parse new repo style configs in style:
664 parse_repo_file(yaml_document_t *doc, yaml_node_t *node)
666 yaml_node_pair_t *pair;
668 pair = node->data.mapping.pairs.start;
669 while (pair < node->data.mapping.pairs.top) {
670 yaml_node_t *key = yaml_document_get_node(doc, pair->key);
671 yaml_node_t *val = yaml_document_get_node(doc, pair->value);
673 if (key->data.scalar.length <= 0) {
678 if (val->type != YAML_MAPPING_NODE) {
683 config_parse(doc, val, CONFFILE_REPO);
690 read_conf_file(const char *confpath, pkg_conf_file_t conftype)
693 yaml_parser_t parser;
697 if ((fp = fopen(confpath, "r")) == NULL) {
699 err(EXIT_FAILURE, "Unable to open configuration "
700 "file %s", confpath);
701 /* no configuration present */
705 yaml_parser_initialize(&parser);
706 yaml_parser_set_input_file(&parser, fp);
707 yaml_parser_load(&parser, &doc);
709 node = yaml_document_get_root_node(&doc);
711 if (node == NULL || node->type != YAML_MAPPING_NODE)
712 warnx("Invalid configuration format, ignoring the "
713 "configuration file %s", confpath);
715 if (conftype == CONFFILE_PKG)
716 config_parse(&doc, node, conftype);
717 else if (conftype == CONFFILE_REPO)
718 parse_repo_file(&doc, node);
721 yaml_document_delete(&doc);
722 yaml_parser_delete(&parser);
728 load_repositories(const char *repodir)
734 char path[MAXPATHLEN];
739 if ((d = opendir(repodir)) == NULL)
742 while ((ent = readdir(d))) {
743 /* Trim out 'repos'. */
744 if ((n = strlen(ent->d_name)) <= 5)
746 p = &ent->d_name[n - 5];
747 if (strcmp(p, ".conf") == 0) {
748 snprintf(path, sizeof(path), "%s%s%s",
750 repodir[strlen(repodir) - 1] == '/' ? "" : "/",
752 if (access(path, F_OK) == 0 &&
753 read_conf_file(path, CONFFILE_REPO)) {
771 const char *localbase;
773 char confpath[MAXPATHLEN];
774 struct config_value *cv;
777 for (i = 0; i < CONFIG_SIZE; i++) {
778 val = getenv(c[i].key);
782 case PKG_CONFIG_LIST:
783 /* Split up comma-separated items from env. */
784 c[i].list = malloc(sizeof(*c[i].list));
785 STAILQ_INIT(c[i].list);
786 for (env_list_item = strtok(val, ",");
787 env_list_item != NULL;
788 env_list_item = strtok(NULL, ",")) {
790 malloc(sizeof(struct config_value));
792 strdup(env_list_item);
793 STAILQ_INSERT_TAIL(c[i].list, cv,
804 /* Read LOCALBASE/etc/pkg.conf first. */
805 localbase = getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE;
806 snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf",
809 if (access(confpath, F_OK) == 0 && read_conf_file(confpath,
813 /* Then read in all repos from REPOS_DIR list of directories. */
814 if (c[REPOS_DIR].list == NULL) {
815 c[REPOS_DIR].list = malloc(sizeof(*c[REPOS_DIR].list));
816 STAILQ_INIT(c[REPOS_DIR].list);
817 cv = malloc(sizeof(struct config_value));
818 cv->value = strdup("/etc/pkg");
819 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
820 cv = malloc(sizeof(struct config_value));
821 if (asprintf(&cv->value, "%s/etc/pkg/repos", localbase) < 0)
823 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
826 STAILQ_FOREACH(cv, c[REPOS_DIR].list, next)
827 if (load_repositories(cv->value))
831 if (c[ABI].val == NULL && c[ABI].value == NULL) {
832 if (pkg_get_myabi(abi, BUFSIZ) != 0)
833 errx(EXIT_FAILURE, "Failed to determine the system "
838 subst_packagesite(c[ABI].value != NULL ? c[ABI].value : c[ABI].val);
844 config_string(pkg_config_key k, const char **val)
846 if (c[k].type != PKG_CONFIG_STRING)
849 if (c[k].value != NULL)
858 config_bool(pkg_config_key k, bool *val)
862 if (c[k].type != PKG_CONFIG_BOOL)
867 if (c[k].value != NULL)
872 if (boolstr_to_bool(value))
879 config_finish(void) {
882 for (i = 0; i < CONFIG_SIZE; i++)