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>
32 #include <sys/queue.h>
34 #include <sys/elf_common.h>
35 #include <sys/endian.h>
36 #include <sys/types.h>
52 #include "elf_tables.h"
55 #define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
59 STAILQ_ENTRY(config_value) next;
67 STAILQ_HEAD(, config_value) *list;
71 static struct config_entry c[] = {
75 URL_SCHEME_PREFIX "http://pkg.FreeBSD.org/${ABI}/latest",
96 [ASSUME_ALWAYS_YES] = {
131 elf_corres_to_string(struct _elf_corres *m, int e)
135 for (i = 0; m[i].string != NULL; i++)
136 if (m[i].elf_nb == e)
137 return (m[i].string);
143 aeabi_parse_arm_attributes(void *data, size_t length)
146 uint8_t *section = data;
148 #define MOVE(len) do { \
149 assert(length >= (len)); \
154 if (length == 0 || *section != 'A')
159 /* Read the section length */
160 if (length < sizeof(sect_len))
163 memcpy(§_len, section, sizeof(sect_len));
166 * The section length should be no longer than the section it is within
168 if (sect_len > length)
171 MOVE(sizeof(sect_len));
173 /* Skip the vendor name */
174 while (length != 0) {
175 if (*section == '\0')
183 while (length != 0) {
187 case 1: /* Tag_File */
189 if (length < sizeof(tag_length))
191 memcpy(&tag_length, section, sizeof(tag_length));
193 case 2: /* Tag_Section */
194 case 3: /* Tag_Symbol */
198 /* At least space for the tag and size */
202 /* Check the tag fits */
203 if (tag_length > length)
206 #define MOVE_TAG(len) do { \
207 assert(tag_length >= (len)); \
209 tag_length -= (len); \
212 MOVE(sizeof(tag_length));
213 tag_length -= sizeof(tag_length);
215 while (tag_length != 0) {
218 assert(tag_length >= length);
224 * These tag values come from:
226 * Addenda to, and Errata in, the ABI for the
227 * ARM Architecture. Release 2.08, section 2.3.
229 if (tag == 6) { /* == Tag_CPU_arch */
234 * We don't support values that require
235 * more than one byte.
240 /* We have an ARMv4 or ARMv5 */
243 else /* We have an ARMv6+ */
245 } else if (tag == 4 || tag == 5 || tag == 32 ||
246 tag == 65 || tag == 67) {
247 while (*section != '\0' && length != 0)
251 /* Skip the last byte */
253 } else if ((tag >= 7 && tag <= 31) || tag == 34 ||
254 tag == 36 || tag == 38 || tag == 42 || tag == 44 ||
255 tag == 64 || tag == 66 || tag == 68 || tag == 70) {
256 /* Skip the uleb128 data */
257 while (*section & (1 << 7) && length != 0)
261 /* Skip the last byte */
275 pkg_get_myabi(char *dest, size_t sz)
282 const char *arch, *abi, *fpu, *endian_corres_str;
283 const char *wordsize_corres_str;
294 if (elf_version(EV_CURRENT) == EV_NONE) {
295 warnx("ELF library initialization failed: %s",
300 if ((fd = open(_PATH_BSHELL, O_RDONLY)) < 0) {
305 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
307 warnx("elf_begin() failed: %s.", elf_errmsg(-1));
311 if (gelf_getehdr(elf, &elfhdr) == NULL) {
313 warn("getehdr() failed: %s.", elf_errmsg(-1));
316 while ((scn = elf_nextscn(elf, scn)) != NULL) {
317 if (gelf_getshdr(scn, &shdr) != &shdr) {
319 warn("getshdr() failed: %s.", elf_errmsg(-1));
323 if (shdr.sh_type == SHT_NOTE)
329 warn("failed to get the note section");
333 data = elf_getdata(scn, NULL);
336 memcpy(¬e, src, sizeof(Elf_Note));
337 src += sizeof(Elf_Note);
338 if (note.n_type == NT_VERSION)
340 src += note.n_namesz + note.n_descsz;
343 src += roundup2(note.n_namesz, 4);
344 if (elfhdr.e_ident[EI_DATA] == ELFDATA2MSB)
345 version = be32dec(src);
347 version = le32dec(src);
349 for (i = 0; osname[i] != '\0'; i++)
350 osname[i] = (char)tolower(osname[i]);
352 wordsize_corres_str = elf_corres_to_string(wordsize_corres,
353 (int)elfhdr.e_ident[EI_CLASS]);
355 arch = elf_corres_to_string(mach_corres, (int) elfhdr.e_machine);
357 snprintf(dest, sz, "%s:%d",
358 osname, version / 100000);
362 switch (elfhdr.e_machine) {
364 endian_corres_str = elf_corres_to_string(endian_corres,
365 (int)elfhdr.e_ident[EI_DATA]);
367 /* FreeBSD doesn't support the hard-float ABI yet */
369 if ((elfhdr.e_flags & 0xFF000000) != 0) {
370 const char *sh_name = NULL;
373 /* This is an EABI file, the conformance level is set */
375 /* Find which TARGET_ARCH we are building for. */
376 elf_getshdrstrndx(elf, &shstrndx);
377 while ((scn = elf_nextscn(elf, scn)) != NULL) {
379 if (gelf_getshdr(scn, &shdr) != &shdr) {
384 sh_name = elf_strptr(elf, shstrndx,
388 if (strcmp(".ARM.attributes", sh_name) == 0)
391 if (scn != NULL && sh_name != NULL) {
392 data = elf_getdata(scn, NULL);
394 * Prior to FreeBSD 10.0 libelf would return
395 * NULL from elf_getdata on the .ARM.attributes
396 * section. As this was the first release to
397 * get armv6 support assume a NULL value means
400 * This assumption can be removed when 9.x
404 arch = aeabi_parse_arm_attributes(
405 data->d_buf, data->d_size);
408 warn("unknown ARM ARCH");
414 warn("Unable to find the .ARM.attributes "
418 } else if (elfhdr.e_ident[EI_OSABI] != ELFOSABI_NONE) {
420 * EABI executables all have this field set to
421 * ELFOSABI_NONE, therefore it must be an oabi file.
426 warn("unknown ARM ABI");
429 snprintf(dest + strlen(dest), sz - strlen(dest),
430 ":%s:%s:%s:%s:%s", arch, wordsize_corres_str,
431 endian_corres_str, abi, fpu);
435 * this is taken from binutils sources:
437 * mapping is figured out from binutils:
438 * gas/config/tc-mips.c
440 switch (elfhdr.e_flags & EF_MIPS_ABI) {
448 if (elfhdr.e_ident[EI_DATA] ==
451 else if (elfhdr.e_ident[EI_DATA] ==
456 endian_corres_str = elf_corres_to_string(endian_corres,
457 (int)elfhdr.e_ident[EI_DATA]);
459 snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s:%s:%s",
460 arch, wordsize_corres_str, endian_corres_str, abi);
463 snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s",
464 arch, wordsize_corres_str);
476 subst_packagesite(const char *abi)
479 const char *variable_string;
482 if (c[PACKAGESITE].value != NULL)
483 oldval = c[PACKAGESITE].value;
485 oldval = c[PACKAGESITE].val;
487 if ((variable_string = strstr(oldval, "${ABI}")) == NULL)
490 newval = sbuf_new_auto();
491 sbuf_bcat(newval, oldval, variable_string - oldval);
492 sbuf_cat(newval, abi);
493 sbuf_cat(newval, variable_string + strlen("${ABI}"));
496 free(c[PACKAGESITE].value);
497 c[PACKAGESITE].value = strdup(sbuf_data(newval));
501 boolstr_to_bool(const char *str)
503 if (str != NULL && (strcasecmp(str, "true") == 0 ||
504 strcasecmp(str, "yes") == 0 || strcasecmp(str, "on") == 0 ||
512 config_parse(yaml_document_t *doc, yaml_node_t *node, pkg_conf_file_t conftype)
514 yaml_node_item_t *item;
515 yaml_node_pair_t *pair;
516 yaml_node_t *key, *val, *item_val;
517 struct sbuf *buf = sbuf_new_auto();
518 struct config_entry *temp_config;
519 struct config_value *cv;
523 pair = node->data.mapping.pairs.start;
525 /* Temporary config for configs that may be disabled. */
526 temp_config = calloc(CONFIG_SIZE, sizeof(struct config_entry));
528 while (pair < node->data.mapping.pairs.top) {
529 key = yaml_document_get_node(doc, pair->key);
530 val = yaml_document_get_node(doc, pair->value);
533 * ignoring silently empty keys can be empty lines
536 if (key->data.scalar.length <= 0) {
542 * silently skip on purpose to allow user to leave
543 * empty lines without complaining
545 if (val->type == YAML_NO_NODE ||
546 (val->type == YAML_SCALAR_NODE &&
547 val->data.scalar.length <= 0)) {
554 if (conftype == CONFFILE_PKG) {
555 for (j = 0; j < strlen(key->data.scalar.value); ++j)
557 toupper(key->data.scalar.value[j]));
559 } else if (conftype == CONFFILE_REPO) {
560 /* The CONFFILE_REPO type is more restrictive. Only
561 parse known elements. */
562 if (strcasecmp(key->data.scalar.value, "url") == 0)
563 sbuf_cpy(buf, "PACKAGESITE");
564 else if (strcasecmp(key->data.scalar.value,
566 sbuf_cpy(buf, "MIRROR_TYPE");
567 else if (strcasecmp(key->data.scalar.value,
568 "signature_type") == 0)
569 sbuf_cpy(buf, "SIGNATURE_TYPE");
570 else if (strcasecmp(key->data.scalar.value,
571 "fingerprints") == 0)
572 sbuf_cpy(buf, "FINGERPRINTS");
573 else if (strcasecmp(key->data.scalar.value,
575 /* Skip disabled repos. */
576 if (!boolstr_to_bool(val->data.scalar.value))
578 } else { /* Skip unknown entries for future use. */
585 for (i = 0; i < CONFIG_SIZE; i++) {
586 if (strcmp(sbuf_data(buf), c[i].key) == 0)
590 /* Silently skip unknown keys to be future compatible. */
591 if (i == CONFIG_SIZE) {
596 /* env has priority over config file */
602 /* Parse sequence value ["item1", "item2"] */
604 case PKG_CONFIG_LIST:
605 if (val->type != YAML_SEQUENCE_NODE) {
606 fprintf(stderr, "Skipping invalid array "
607 "value for %s.\n", c[i].key);
611 item = val->data.sequence.items.start;
612 temp_config[i].list =
613 malloc(sizeof(*temp_config[i].list));
614 STAILQ_INIT(temp_config[i].list);
616 while (item < val->data.sequence.items.top) {
617 item_val = yaml_document_get_node(doc, *item);
618 if (item_val->type != YAML_SCALAR_NODE) {
622 cv = malloc(sizeof(struct config_value));
624 strdup(item_val->data.scalar.value);
625 STAILQ_INSERT_TAIL(temp_config[i].list, cv,
631 /* Normal string value. */
632 temp_config[i].value = strdup(val->data.scalar.value);
638 /* Repo is enabled, copy over all settings from temp_config. */
639 for (i = 0; i < CONFIG_SIZE; i++) {
643 case PKG_CONFIG_LIST:
644 c[i].list = temp_config[i].list;
647 c[i].value = temp_config[i].value;
658 * Parse new repo style configs in style:
665 parse_repo_file(yaml_document_t *doc, yaml_node_t *node)
667 yaml_node_pair_t *pair;
669 pair = node->data.mapping.pairs.start;
670 while (pair < node->data.mapping.pairs.top) {
671 yaml_node_t *key = yaml_document_get_node(doc, pair->key);
672 yaml_node_t *val = yaml_document_get_node(doc, pair->value);
674 if (key->data.scalar.length <= 0) {
679 if (val->type != YAML_MAPPING_NODE) {
684 config_parse(doc, val, CONFFILE_REPO);
691 read_conf_file(const char *confpath, pkg_conf_file_t conftype)
694 yaml_parser_t parser;
698 if ((fp = fopen(confpath, "r")) == NULL) {
700 err(EXIT_FAILURE, "Unable to open configuration "
701 "file %s", confpath);
702 /* no configuration present */
706 yaml_parser_initialize(&parser);
707 yaml_parser_set_input_file(&parser, fp);
708 yaml_parser_load(&parser, &doc);
710 node = yaml_document_get_root_node(&doc);
712 if (node == NULL || node->type != YAML_MAPPING_NODE)
713 warnx("Invalid configuration format, ignoring the "
714 "configuration file %s", confpath);
716 if (conftype == CONFFILE_PKG)
717 config_parse(&doc, node, conftype);
718 else if (conftype == CONFFILE_REPO)
719 parse_repo_file(&doc, node);
722 yaml_document_delete(&doc);
723 yaml_parser_delete(&parser);
729 load_repositories(const char *repodir)
735 char path[MAXPATHLEN];
740 if ((d = opendir(repodir)) == NULL)
743 while ((ent = readdir(d))) {
744 /* Trim out 'repos'. */
745 if ((n = strlen(ent->d_name)) <= 5)
747 p = &ent->d_name[n - 5];
748 if (strcmp(p, ".conf") == 0) {
749 snprintf(path, sizeof(path), "%s%s%s",
751 repodir[strlen(repodir) - 1] == '/' ? "" : "/",
753 if (access(path, F_OK) == 0 &&
754 read_conf_file(path, CONFFILE_REPO)) {
772 const char *localbase;
774 char confpath[MAXPATHLEN];
775 struct config_value *cv;
778 for (i = 0; i < CONFIG_SIZE; i++) {
779 val = getenv(c[i].key);
783 case PKG_CONFIG_LIST:
784 /* Split up comma-separated items from env. */
785 c[i].list = malloc(sizeof(*c[i].list));
786 STAILQ_INIT(c[i].list);
787 for (env_list_item = strtok(val, ",");
788 env_list_item != NULL;
789 env_list_item = strtok(NULL, ",")) {
791 malloc(sizeof(struct config_value));
793 strdup(env_list_item);
794 STAILQ_INSERT_TAIL(c[i].list, cv,
805 /* Read LOCALBASE/etc/pkg.conf first. */
806 localbase = getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE;
807 snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf",
810 if (access(confpath, F_OK) == 0 && read_conf_file(confpath,
814 /* Then read in all repos from REPOS_DIR list of directories. */
815 if (c[REPOS_DIR].list == NULL) {
816 c[REPOS_DIR].list = malloc(sizeof(*c[REPOS_DIR].list));
817 STAILQ_INIT(c[REPOS_DIR].list);
818 cv = malloc(sizeof(struct config_value));
819 cv->value = strdup("/etc/pkg");
820 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
821 cv = malloc(sizeof(struct config_value));
822 if (asprintf(&cv->value, "%s/etc/pkg/repos", localbase) < 0)
824 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
827 STAILQ_FOREACH(cv, c[REPOS_DIR].list, next)
828 if (load_repositories(cv->value))
832 if (c[ABI].val == NULL && c[ABI].value == NULL) {
833 if (pkg_get_myabi(abi, BUFSIZ) != 0)
834 errx(EXIT_FAILURE, "Failed to determine the system "
839 subst_packagesite(c[ABI].value != NULL ? c[ABI].value : c[ABI].val);
845 config_string(pkg_config_key k, const char **val)
847 if (c[k].type != PKG_CONFIG_STRING)
850 if (c[k].value != NULL)
859 config_bool(pkg_config_key k, bool *val)
863 if (c[k].type != PKG_CONFIG_BOOL)
868 if (c[k].value != NULL)
873 if (boolstr_to_bool(value))
880 config_finish(void) {
883 for (i = 0; i < CONFIG_SIZE; i++)