2 * Copyright (c) 2014 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;
69 bool main_only; /* Only set in pkg.conf. */
72 static struct config_entry c[] = {
76 URL_SCHEME_PREFIX "http://pkg.FreeBSD.org/${ABI}/latest",
100 [ASSUME_ALWAYS_YES] = {
139 elf_corres_to_string(struct _elf_corres *m, int e)
143 for (i = 0; m[i].string != NULL; i++)
144 if (m[i].elf_nb == e)
145 return (m[i].string);
151 aeabi_parse_arm_attributes(void *data, size_t length)
154 uint8_t *section = data;
156 #define MOVE(len) do { \
157 assert(length >= (len)); \
162 if (length == 0 || *section != 'A')
167 /* Read the section length */
168 if (length < sizeof(sect_len))
171 memcpy(§_len, section, sizeof(sect_len));
174 * The section length should be no longer than the section it is within
176 if (sect_len > length)
179 MOVE(sizeof(sect_len));
181 /* Skip the vendor name */
182 while (length != 0) {
183 if (*section == '\0')
191 while (length != 0) {
195 case 1: /* Tag_File */
197 if (length < sizeof(tag_length))
199 memcpy(&tag_length, section, sizeof(tag_length));
201 case 2: /* Tag_Section */
202 case 3: /* Tag_Symbol */
206 /* At least space for the tag and size */
210 /* Check the tag fits */
211 if (tag_length > length)
214 #define MOVE_TAG(len) do { \
215 assert(tag_length >= (len)); \
217 tag_length -= (len); \
220 MOVE(sizeof(tag_length));
221 tag_length -= sizeof(tag_length);
223 while (tag_length != 0) {
226 assert(tag_length >= length);
232 * These tag values come from:
234 * Addenda to, and Errata in, the ABI for the
235 * ARM Architecture. Release 2.08, section 2.3.
237 if (tag == 6) { /* == Tag_CPU_arch */
242 * We don't support values that require
243 * more than one byte.
248 /* We have an ARMv4 or ARMv5 */
251 else /* We have an ARMv6+ */
253 } else if (tag == 4 || tag == 5 || tag == 32 ||
254 tag == 65 || tag == 67) {
255 while (*section != '\0' && length != 0)
259 /* Skip the last byte */
261 } else if ((tag >= 7 && tag <= 31) || tag == 34 ||
262 tag == 36 || tag == 38 || tag == 42 || tag == 44 ||
263 tag == 64 || tag == 66 || tag == 68 || tag == 70) {
264 /* Skip the uleb128 data */
265 while (*section & (1 << 7) && length != 0)
269 /* Skip the last byte */
283 pkg_get_myabi(char *dest, size_t sz)
290 const char *arch, *abi, *fpu, *endian_corres_str;
291 const char *wordsize_corres_str;
302 if (elf_version(EV_CURRENT) == EV_NONE) {
303 warnx("ELF library initialization failed: %s",
308 if ((fd = open(_PATH_BSHELL, O_RDONLY)) < 0) {
313 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
315 warnx("elf_begin() failed: %s.", elf_errmsg(-1));
319 if (gelf_getehdr(elf, &elfhdr) == NULL) {
321 warn("getehdr() failed: %s.", elf_errmsg(-1));
324 while ((scn = elf_nextscn(elf, scn)) != NULL) {
325 if (gelf_getshdr(scn, &shdr) != &shdr) {
327 warn("getshdr() failed: %s.", elf_errmsg(-1));
331 if (shdr.sh_type == SHT_NOTE)
337 warn("failed to get the note section");
341 data = elf_getdata(scn, NULL);
344 memcpy(¬e, src, sizeof(Elf_Note));
345 src += sizeof(Elf_Note);
346 if (note.n_type == NT_VERSION)
348 src += note.n_namesz + note.n_descsz;
351 src += roundup2(note.n_namesz, 4);
352 if (elfhdr.e_ident[EI_DATA] == ELFDATA2MSB)
353 version = be32dec(src);
355 version = le32dec(src);
357 for (i = 0; osname[i] != '\0'; i++)
358 osname[i] = (char)tolower(osname[i]);
360 wordsize_corres_str = elf_corres_to_string(wordsize_corres,
361 (int)elfhdr.e_ident[EI_CLASS]);
363 arch = elf_corres_to_string(mach_corres, (int) elfhdr.e_machine);
365 snprintf(dest, sz, "%s:%d",
366 osname, version / 100000);
370 switch (elfhdr.e_machine) {
372 endian_corres_str = elf_corres_to_string(endian_corres,
373 (int)elfhdr.e_ident[EI_DATA]);
375 /* FreeBSD doesn't support the hard-float ABI yet */
377 if ((elfhdr.e_flags & 0xFF000000) != 0) {
378 const char *sh_name = NULL;
381 /* This is an EABI file, the conformance level is set */
383 /* Find which TARGET_ARCH we are building for. */
384 elf_getshdrstrndx(elf, &shstrndx);
385 while ((scn = elf_nextscn(elf, scn)) != NULL) {
387 if (gelf_getshdr(scn, &shdr) != &shdr) {
392 sh_name = elf_strptr(elf, shstrndx,
396 if (strcmp(".ARM.attributes", sh_name) == 0)
399 if (scn != NULL && sh_name != NULL) {
400 data = elf_getdata(scn, NULL);
402 * Prior to FreeBSD 10.0 libelf would return
403 * NULL from elf_getdata on the .ARM.attributes
404 * section. As this was the first release to
405 * get armv6 support assume a NULL value means
408 * This assumption can be removed when 9.x
412 arch = aeabi_parse_arm_attributes(
413 data->d_buf, data->d_size);
416 warn("unknown ARM ARCH");
422 warn("Unable to find the .ARM.attributes "
426 } else if (elfhdr.e_ident[EI_OSABI] != ELFOSABI_NONE) {
428 * EABI executables all have this field set to
429 * ELFOSABI_NONE, therefore it must be an oabi file.
434 warn("unknown ARM ABI");
437 snprintf(dest + strlen(dest), sz - strlen(dest),
438 ":%s:%s:%s:%s:%s", arch, wordsize_corres_str,
439 endian_corres_str, abi, fpu);
443 * this is taken from binutils sources:
445 * mapping is figured out from binutils:
446 * gas/config/tc-mips.c
448 switch (elfhdr.e_flags & EF_MIPS_ABI) {
456 if (elfhdr.e_ident[EI_DATA] ==
459 else if (elfhdr.e_ident[EI_DATA] ==
464 endian_corres_str = elf_corres_to_string(endian_corres,
465 (int)elfhdr.e_ident[EI_DATA]);
467 snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s:%s:%s",
468 arch, wordsize_corres_str, endian_corres_str, abi);
471 snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s",
472 arch, wordsize_corres_str);
484 subst_packagesite(const char *abi)
487 const char *variable_string;
490 if (c[PACKAGESITE].value != NULL)
491 oldval = c[PACKAGESITE].value;
493 oldval = c[PACKAGESITE].val;
495 if ((variable_string = strstr(oldval, "${ABI}")) == NULL)
498 newval = sbuf_new_auto();
499 sbuf_bcat(newval, oldval, variable_string - oldval);
500 sbuf_cat(newval, abi);
501 sbuf_cat(newval, variable_string + strlen("${ABI}"));
504 free(c[PACKAGESITE].value);
505 c[PACKAGESITE].value = strdup(sbuf_data(newval));
509 boolstr_to_bool(const char *str)
511 if (str != NULL && (strcasecmp(str, "true") == 0 ||
512 strcasecmp(str, "yes") == 0 || strcasecmp(str, "on") == 0 ||
520 config_parse(const ucl_object_t *obj, pkg_conf_file_t conftype)
522 struct sbuf *buf = sbuf_new_auto();
523 const ucl_object_t *cur, *seq;
524 ucl_object_iter_t it = NULL, itseq = NULL;
525 struct config_entry *temp_config;
526 struct config_value *cv;
531 /* Temporary config for configs that may be disabled. */
532 temp_config = calloc(CONFIG_SIZE, sizeof(struct config_entry));
534 while ((cur = ucl_iterate_object(obj, &it, true))) {
535 key = ucl_object_key(cur);
540 if (conftype == CONFFILE_PKG) {
541 for (j = 0; j < strlen(key); ++j)
542 sbuf_putc(buf, key[j]);
544 } else if (conftype == CONFFILE_REPO) {
545 if (strcasecmp(key, "url") == 0)
546 sbuf_cpy(buf, "PACKAGESITE");
547 else if (strcasecmp(key, "mirror_type") == 0)
548 sbuf_cpy(buf, "MIRROR_TYPE");
549 else if (strcasecmp(key, "signature_type") == 0)
550 sbuf_cpy(buf, "SIGNATURE_TYPE");
551 else if (strcasecmp(key, "fingerprints") == 0)
552 sbuf_cpy(buf, "FINGERPRINTS");
553 else if (strcasecmp(key, "enabled") == 0) {
554 if ((cur->type != UCL_BOOLEAN) ||
555 !ucl_object_toboolean(cur))
562 for (i = 0; i < CONFIG_SIZE; i++) {
563 if (strcmp(sbuf_data(buf), c[i].key) == 0)
567 /* Silently skip unknown keys to be future compatible. */
568 if (i == CONFIG_SIZE)
571 /* env has priority over config file */
575 /* Parse sequence value ["item1", "item2"] */
577 case PKG_CONFIG_LIST:
578 if (cur->type != UCL_ARRAY) {
579 warnx("Skipping invalid array "
580 "value for %s.\n", c[i].key);
583 temp_config[i].list =
584 malloc(sizeof(*temp_config[i].list));
585 STAILQ_INIT(temp_config[i].list);
587 while ((seq = ucl_iterate_object(cur, &itseq, true))) {
588 if (seq->type != UCL_STRING)
590 cv = malloc(sizeof(struct config_value));
592 strdup(ucl_object_tostring(seq));
593 STAILQ_INSERT_TAIL(temp_config[i].list, cv,
597 case PKG_CONFIG_BOOL:
598 temp_config[i].value =
599 strdup(ucl_object_toboolean(cur) ? "yes" : "no");
602 /* Normal string value. */
603 temp_config[i].value = strdup(ucl_object_tostring(cur));
608 /* Repo is enabled, copy over all settings from temp_config. */
609 for (i = 0; i < CONFIG_SIZE; i++) {
612 /* Prevent overriding ABI, ASSUME_ALWAYS_YES, etc. */
613 if (conftype != CONFFILE_PKG && c[i].main_only == true)
616 case PKG_CONFIG_LIST:
617 c[i].list = temp_config[i].list;
620 c[i].value = temp_config[i].value;
631 * Parse new repo style configs in style:
638 parse_repo_file(ucl_object_t *obj)
640 ucl_object_iter_t it = NULL;
641 const ucl_object_t *cur;
644 while ((cur = ucl_iterate_object(obj, &it, true))) {
645 key = ucl_object_key(cur);
650 if (cur->type != UCL_OBJECT)
653 config_parse(cur, CONFFILE_REPO);
659 read_conf_file(const char *confpath, pkg_conf_file_t conftype)
661 struct ucl_parser *p;
662 ucl_object_t *obj = NULL;
664 p = ucl_parser_new(0);
666 if (!ucl_parser_add_file(p, confpath)) {
668 errx(EXIT_FAILURE, "Unable to parse configuration "
669 "file %s: %s", confpath, ucl_parser_get_error(p));
671 /* no configuration present */
675 obj = ucl_parser_get_object(p);
676 if (obj->type != UCL_OBJECT)
677 warnx("Invalid configuration format, ignoring the "
678 "configuration file %s", confpath);
680 if (conftype == CONFFILE_PKG)
681 config_parse(obj, conftype);
682 else if (conftype == CONFFILE_REPO)
683 parse_repo_file(obj);
686 ucl_object_unref(obj);
693 load_repositories(const char *repodir)
699 char path[MAXPATHLEN];
704 if ((d = opendir(repodir)) == NULL)
707 while ((ent = readdir(d))) {
708 /* Trim out 'repos'. */
709 if ((n = strlen(ent->d_name)) <= 5)
711 p = &ent->d_name[n - 5];
712 if (strcmp(p, ".conf") == 0) {
713 snprintf(path, sizeof(path), "%s%s%s",
715 repodir[strlen(repodir) - 1] == '/' ? "" : "/",
717 if (access(path, F_OK) == 0 &&
718 read_conf_file(path, CONFFILE_REPO)) {
736 const char *localbase;
738 char confpath[MAXPATHLEN];
739 struct config_value *cv;
742 for (i = 0; i < CONFIG_SIZE; i++) {
743 val = getenv(c[i].key);
747 case PKG_CONFIG_LIST:
748 /* Split up comma-separated items from env. */
749 c[i].list = malloc(sizeof(*c[i].list));
750 STAILQ_INIT(c[i].list);
751 for (env_list_item = strtok(val, ",");
752 env_list_item != NULL;
753 env_list_item = strtok(NULL, ",")) {
755 malloc(sizeof(struct config_value));
757 strdup(env_list_item);
758 STAILQ_INSERT_TAIL(c[i].list, cv,
769 /* Read LOCALBASE/etc/pkg.conf first. */
770 localbase = getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE;
771 snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf",
774 if (access(confpath, F_OK) == 0 && read_conf_file(confpath,
778 /* Then read in all repos from REPOS_DIR list of directories. */
779 if (c[REPOS_DIR].list == NULL) {
780 c[REPOS_DIR].list = malloc(sizeof(*c[REPOS_DIR].list));
781 STAILQ_INIT(c[REPOS_DIR].list);
782 cv = malloc(sizeof(struct config_value));
783 cv->value = strdup("/etc/pkg");
784 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
785 cv = malloc(sizeof(struct config_value));
786 if (asprintf(&cv->value, "%s/etc/pkg/repos", localbase) < 0)
788 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
791 STAILQ_FOREACH(cv, c[REPOS_DIR].list, next)
792 if (load_repositories(cv->value))
796 if (c[ABI].val == NULL && c[ABI].value == NULL) {
797 if (pkg_get_myabi(abi, BUFSIZ) != 0)
798 errx(EXIT_FAILURE, "Failed to determine the system "
803 subst_packagesite(c[ABI].value != NULL ? c[ABI].value : c[ABI].val);
809 config_string(pkg_config_key k, const char **val)
811 if (c[k].type != PKG_CONFIG_STRING)
814 if (c[k].value != NULL)
823 config_bool(pkg_config_key k, bool *val)
827 if (c[k].type != PKG_CONFIG_BOOL)
832 if (c[k].value != NULL)
837 if (boolstr_to_bool(value))
844 config_finish(void) {
847 for (i = 0; i < CONFIG_SIZE; i++)