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] = {
148 elf_corres_to_string(struct _elf_corres *m, int e)
152 for (i = 0; m[i].string != NULL; i++)
153 if (m[i].elf_nb == e)
154 return (m[i].string);
160 aeabi_parse_arm_attributes(void *data, size_t length)
163 uint8_t *section = data;
165 #define MOVE(len) do { \
166 assert(length >= (len)); \
171 if (length == 0 || *section != 'A')
176 /* Read the section length */
177 if (length < sizeof(sect_len))
180 memcpy(§_len, section, sizeof(sect_len));
183 * The section length should be no longer than the section it is within
185 if (sect_len > length)
188 MOVE(sizeof(sect_len));
190 /* Skip the vendor name */
191 while (length != 0) {
192 if (*section == '\0')
200 while (length != 0) {
204 case 1: /* Tag_File */
206 if (length < sizeof(tag_length))
208 memcpy(&tag_length, section, sizeof(tag_length));
210 case 2: /* Tag_Section */
211 case 3: /* Tag_Symbol */
215 /* At least space for the tag and size */
219 /* Check the tag fits */
220 if (tag_length > length)
223 #define MOVE_TAG(len) do { \
224 assert(tag_length >= (len)); \
226 tag_length -= (len); \
229 MOVE(sizeof(tag_length));
230 tag_length -= sizeof(tag_length);
232 while (tag_length != 0) {
235 assert(tag_length >= length);
241 * These tag values come from:
243 * Addenda to, and Errata in, the ABI for the
244 * ARM Architecture. Release 2.08, section 2.3.
246 if (tag == 6) { /* == Tag_CPU_arch */
251 * We don't support values that require
252 * more than one byte.
257 /* We have an ARMv4 or ARMv5 */
260 else /* We have an ARMv6+ */
262 } else if (tag == 4 || tag == 5 || tag == 32 ||
263 tag == 65 || tag == 67) {
264 while (*section != '\0' && length != 0)
268 /* Skip the last byte */
270 } else if ((tag >= 7 && tag <= 31) || tag == 34 ||
271 tag == 36 || tag == 38 || tag == 42 || tag == 44 ||
272 tag == 64 || tag == 66 || tag == 68 || tag == 70) {
273 /* Skip the uleb128 data */
274 while (*section & (1 << 7) && length != 0)
278 /* Skip the last byte */
292 pkg_get_myabi(char *dest, size_t sz)
299 const char *arch, *abi, *fpu, *endian_corres_str;
300 const char *wordsize_corres_str;
311 if (elf_version(EV_CURRENT) == EV_NONE) {
312 warnx("ELF library initialization failed: %s",
317 if ((fd = open(_PATH_BSHELL, O_RDONLY)) < 0) {
322 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
324 warnx("elf_begin() failed: %s.", elf_errmsg(-1));
328 if (gelf_getehdr(elf, &elfhdr) == NULL) {
330 warn("getehdr() failed: %s.", elf_errmsg(-1));
333 while ((scn = elf_nextscn(elf, scn)) != NULL) {
334 if (gelf_getshdr(scn, &shdr) != &shdr) {
336 warn("getshdr() failed: %s.", elf_errmsg(-1));
340 if (shdr.sh_type == SHT_NOTE)
346 warn("failed to get the note section");
350 data = elf_getdata(scn, NULL);
353 memcpy(¬e, src, sizeof(Elf_Note));
354 src += sizeof(Elf_Note);
355 if (note.n_type == NT_VERSION)
357 src += note.n_namesz + note.n_descsz;
360 src += roundup2(note.n_namesz, 4);
361 if (elfhdr.e_ident[EI_DATA] == ELFDATA2MSB)
362 version = be32dec(src);
364 version = le32dec(src);
366 for (i = 0; osname[i] != '\0'; i++)
367 osname[i] = (char)tolower(osname[i]);
369 wordsize_corres_str = elf_corres_to_string(wordsize_corres,
370 (int)elfhdr.e_ident[EI_CLASS]);
372 arch = elf_corres_to_string(mach_corres, (int) elfhdr.e_machine);
374 snprintf(dest, sz, "%s:%d",
375 osname, version / 100000);
379 switch (elfhdr.e_machine) {
381 endian_corres_str = elf_corres_to_string(endian_corres,
382 (int)elfhdr.e_ident[EI_DATA]);
384 /* FreeBSD doesn't support the hard-float ABI yet */
386 if ((elfhdr.e_flags & 0xFF000000) != 0) {
387 const char *sh_name = NULL;
390 /* This is an EABI file, the conformance level is set */
392 /* Find which TARGET_ARCH we are building for. */
393 elf_getshdrstrndx(elf, &shstrndx);
394 while ((scn = elf_nextscn(elf, scn)) != NULL) {
396 if (gelf_getshdr(scn, &shdr) != &shdr) {
401 sh_name = elf_strptr(elf, shstrndx,
405 if (strcmp(".ARM.attributes", sh_name) == 0)
408 if (scn != NULL && sh_name != NULL) {
409 data = elf_getdata(scn, NULL);
411 * Prior to FreeBSD 10.0 libelf would return
412 * NULL from elf_getdata on the .ARM.attributes
413 * section. As this was the first release to
414 * get armv6 support assume a NULL value means
417 * This assumption can be removed when 9.x
421 arch = aeabi_parse_arm_attributes(
422 data->d_buf, data->d_size);
425 warn("unknown ARM ARCH");
431 warn("Unable to find the .ARM.attributes "
435 } else if (elfhdr.e_ident[EI_OSABI] != ELFOSABI_NONE) {
437 * EABI executables all have this field set to
438 * ELFOSABI_NONE, therefore it must be an oabi file.
443 warn("unknown ARM ABI");
446 snprintf(dest + strlen(dest), sz - strlen(dest),
447 ":%s:%s:%s:%s:%s", arch, wordsize_corres_str,
448 endian_corres_str, abi, fpu);
452 * this is taken from binutils sources:
454 * mapping is figured out from binutils:
455 * gas/config/tc-mips.c
457 switch (elfhdr.e_flags & EF_MIPS_ABI) {
465 if (elfhdr.e_ident[EI_DATA] ==
468 else if (elfhdr.e_ident[EI_DATA] ==
473 endian_corres_str = elf_corres_to_string(endian_corres,
474 (int)elfhdr.e_ident[EI_DATA]);
476 snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s:%s:%s",
477 arch, wordsize_corres_str, endian_corres_str, abi);
480 snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s",
481 arch, wordsize_corres_str);
493 subst_packagesite(const char *abi)
496 const char *variable_string;
499 if (c[PACKAGESITE].value != NULL)
500 oldval = c[PACKAGESITE].value;
502 oldval = c[PACKAGESITE].val;
504 if ((variable_string = strstr(oldval, "${ABI}")) == NULL)
507 newval = sbuf_new_auto();
508 sbuf_bcat(newval, oldval, variable_string - oldval);
509 sbuf_cat(newval, abi);
510 sbuf_cat(newval, variable_string + strlen("${ABI}"));
513 free(c[PACKAGESITE].value);
514 c[PACKAGESITE].value = strdup(sbuf_data(newval));
518 boolstr_to_bool(const char *str)
520 if (str != NULL && (strcasecmp(str, "true") == 0 ||
521 strcasecmp(str, "yes") == 0 || strcasecmp(str, "on") == 0 ||
529 config_parse(const ucl_object_t *obj, pkg_conf_file_t conftype)
531 struct sbuf *buf = sbuf_new_auto();
532 const ucl_object_t *cur, *seq;
533 ucl_object_iter_t it = NULL, itseq = NULL;
534 struct config_entry *temp_config;
535 struct config_value *cv;
540 /* Temporary config for configs that may be disabled. */
541 temp_config = calloc(CONFIG_SIZE, sizeof(struct config_entry));
543 while ((cur = ucl_iterate_object(obj, &it, true))) {
544 key = ucl_object_key(cur);
549 if (conftype == CONFFILE_PKG) {
550 for (j = 0; j < strlen(key); ++j)
551 sbuf_putc(buf, key[j]);
553 } else if (conftype == CONFFILE_REPO) {
554 if (strcasecmp(key, "url") == 0)
555 sbuf_cpy(buf, "PACKAGESITE");
556 else if (strcasecmp(key, "mirror_type") == 0)
557 sbuf_cpy(buf, "MIRROR_TYPE");
558 else if (strcasecmp(key, "signature_type") == 0)
559 sbuf_cpy(buf, "SIGNATURE_TYPE");
560 else if (strcasecmp(key, "fingerprints") == 0)
561 sbuf_cpy(buf, "FINGERPRINTS");
562 else if (strcasecmp(key, "pubkey") == 0)
563 sbuf_cpy(buf, "PUBKEY");
564 else if (strcasecmp(key, "enabled") == 0) {
565 if ((cur->type != UCL_BOOLEAN) ||
566 !ucl_object_toboolean(cur))
573 for (i = 0; i < CONFIG_SIZE; i++) {
574 if (strcmp(sbuf_data(buf), c[i].key) == 0)
578 /* Silently skip unknown keys to be future compatible. */
579 if (i == CONFIG_SIZE)
582 /* env has priority over config file */
586 /* Parse sequence value ["item1", "item2"] */
588 case PKG_CONFIG_LIST:
589 if (cur->type != UCL_ARRAY) {
590 warnx("Skipping invalid array "
591 "value for %s.\n", c[i].key);
594 temp_config[i].list =
595 malloc(sizeof(*temp_config[i].list));
596 STAILQ_INIT(temp_config[i].list);
598 while ((seq = ucl_iterate_object(cur, &itseq, true))) {
599 if (seq->type != UCL_STRING)
601 cv = malloc(sizeof(struct config_value));
603 strdup(ucl_object_tostring(seq));
604 STAILQ_INSERT_TAIL(temp_config[i].list, cv,
608 case PKG_CONFIG_BOOL:
609 temp_config[i].value =
610 strdup(ucl_object_toboolean(cur) ? "yes" : "no");
613 /* Normal string value. */
614 temp_config[i].value = strdup(ucl_object_tostring(cur));
619 /* Repo is enabled, copy over all settings from temp_config. */
620 for (i = 0; i < CONFIG_SIZE; i++) {
623 /* Prevent overriding ABI, ASSUME_ALWAYS_YES, etc. */
624 if (conftype != CONFFILE_PKG && c[i].main_only == true)
627 case PKG_CONFIG_LIST:
628 c[i].list = temp_config[i].list;
631 c[i].value = temp_config[i].value;
642 * Parse new repo style configs in style:
649 parse_repo_file(ucl_object_t *obj)
651 ucl_object_iter_t it = NULL;
652 const ucl_object_t *cur;
655 while ((cur = ucl_iterate_object(obj, &it, true))) {
656 key = ucl_object_key(cur);
661 if (cur->type != UCL_OBJECT)
664 config_parse(cur, CONFFILE_REPO);
670 read_conf_file(const char *confpath, pkg_conf_file_t conftype)
672 struct ucl_parser *p;
673 ucl_object_t *obj = NULL;
675 p = ucl_parser_new(0);
677 if (!ucl_parser_add_file(p, confpath)) {
679 errx(EXIT_FAILURE, "Unable to parse configuration "
680 "file %s: %s", confpath, ucl_parser_get_error(p));
682 /* no configuration present */
686 obj = ucl_parser_get_object(p);
687 if (obj->type != UCL_OBJECT)
688 warnx("Invalid configuration format, ignoring the "
689 "configuration file %s", confpath);
691 if (conftype == CONFFILE_PKG)
692 config_parse(obj, conftype);
693 else if (conftype == CONFFILE_REPO)
694 parse_repo_file(obj);
697 ucl_object_unref(obj);
704 load_repositories(const char *repodir)
710 char path[MAXPATHLEN];
715 if ((d = opendir(repodir)) == NULL)
718 while ((ent = readdir(d))) {
719 /* Trim out 'repos'. */
720 if ((n = strlen(ent->d_name)) <= 5)
722 p = &ent->d_name[n - 5];
723 if (strcmp(p, ".conf") == 0) {
724 snprintf(path, sizeof(path), "%s%s%s",
726 repodir[strlen(repodir) - 1] == '/' ? "" : "/",
728 if (access(path, F_OK) == 0 &&
729 read_conf_file(path, CONFFILE_REPO)) {
747 const char *localbase;
749 char confpath[MAXPATHLEN];
750 struct config_value *cv;
753 for (i = 0; i < CONFIG_SIZE; i++) {
754 val = getenv(c[i].key);
758 case PKG_CONFIG_LIST:
759 /* Split up comma-separated items from env. */
760 c[i].list = malloc(sizeof(*c[i].list));
761 STAILQ_INIT(c[i].list);
762 for (env_list_item = strtok(val, ",");
763 env_list_item != NULL;
764 env_list_item = strtok(NULL, ",")) {
766 malloc(sizeof(struct config_value));
768 strdup(env_list_item);
769 STAILQ_INSERT_TAIL(c[i].list, cv,
780 /* Read LOCALBASE/etc/pkg.conf first. */
781 localbase = getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE;
782 snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf",
785 if (access(confpath, F_OK) == 0 && read_conf_file(confpath,
789 /* Then read in all repos from REPOS_DIR list of directories. */
790 if (c[REPOS_DIR].list == NULL) {
791 c[REPOS_DIR].list = malloc(sizeof(*c[REPOS_DIR].list));
792 STAILQ_INIT(c[REPOS_DIR].list);
793 cv = malloc(sizeof(struct config_value));
794 cv->value = strdup("/etc/pkg");
795 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
796 cv = malloc(sizeof(struct config_value));
797 if (asprintf(&cv->value, "%s/etc/pkg/repos", localbase) < 0)
799 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
802 STAILQ_FOREACH(cv, c[REPOS_DIR].list, next)
803 if (load_repositories(cv->value))
807 if (c[ABI].val == NULL && c[ABI].value == NULL) {
808 if (pkg_get_myabi(abi, BUFSIZ) != 0)
809 errx(EXIT_FAILURE, "Failed to determine the system "
814 subst_packagesite(c[ABI].value != NULL ? c[ABI].value : c[ABI].val);
820 config_string(pkg_config_key k, const char **val)
822 if (c[k].type != PKG_CONFIG_STRING)
825 if (c[k].value != NULL)
834 config_bool(pkg_config_key k, bool *val)
838 if (c[k].type != PKG_CONFIG_BOOL)
843 if (c[k].value != NULL)
848 if (boolstr_to_bool(value))
855 config_finish(void) {
858 for (i = 0; i < CONFIG_SIZE; i++)