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;
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(ucl_object_t *obj, pkg_conf_file_t conftype)
514 struct sbuf *buf = sbuf_new_auto();
515 ucl_object_t *cur, *seq;
516 ucl_object_iter_t it = NULL, itseq = NULL;
517 struct config_entry *temp_config;
518 struct config_value *cv;
523 /* Temporary config for configs that may be disabled. */
524 temp_config = calloc(CONFIG_SIZE, sizeof(struct config_entry));
526 while ((cur = ucl_iterate_object(obj, &it, true))) {
527 key = ucl_object_key(cur);
532 if (conftype == CONFFILE_PKG) {
533 for (j = 0; j < strlen(key); ++j)
534 sbuf_putc(buf, key[j]);
536 } else if (conftype == CONFFILE_REPO) {
537 if (strcasecmp(key, "url") == 0)
538 sbuf_cpy(buf, "PACKAGESITE");
539 else if (strcasecmp(key, "mirror_type") == 0)
540 sbuf_cpy(buf, "MIRROR_TYPE");
541 else if (strcasecmp(key, "signature_type") == 0)
542 sbuf_cpy(buf, "SIGNATURE_TYPE");
543 else if (strcasecmp(key, "fingerprints") == 0)
544 sbuf_cpy(buf, "FINGERPRINTS");
545 else if (strcasecmp(key, "enabled") == 0) {
546 if ((cur->type != UCL_BOOLEAN) ||
547 !ucl_object_toboolean(cur))
554 for (i = 0; i < CONFIG_SIZE; i++) {
555 if (strcmp(sbuf_data(buf), c[i].key) == 0)
559 /* Silently skip unknown keys to be future compatible. */
560 if (i == CONFIG_SIZE)
563 /* env has priority over config file */
567 /* Parse sequence value ["item1", "item2"] */
569 case PKG_CONFIG_LIST:
570 if (cur->type != UCL_ARRAY) {
571 warnx("Skipping invalid array "
572 "value for %s.\n", c[i].key);
575 temp_config[i].list =
576 malloc(sizeof(*temp_config[i].list));
577 STAILQ_INIT(temp_config[i].list);
579 while ((seq = ucl_iterate_object(cur, &itseq, true))) {
580 if (seq->type != UCL_STRING)
582 cv = malloc(sizeof(struct config_value));
584 strdup(ucl_object_tostring(seq));
585 STAILQ_INSERT_TAIL(temp_config[i].list, cv,
590 /* Normal string value. */
591 temp_config[i].value = strdup(ucl_object_tostring(cur));
596 /* Repo is enabled, copy over all settings from temp_config. */
597 for (i = 0; i < CONFIG_SIZE; i++) {
601 case PKG_CONFIG_LIST:
602 c[i].list = temp_config[i].list;
605 c[i].value = temp_config[i].value;
616 * Parse new repo style configs in style:
623 parse_repo_file(ucl_object_t *obj)
625 ucl_object_iter_t it = NULL;
629 while ((cur = ucl_iterate_object(obj, &it, true))) {
630 key = ucl_object_key(cur);
635 if (cur->type != UCL_OBJECT)
638 config_parse(cur, CONFFILE_REPO);
644 read_conf_file(const char *confpath, pkg_conf_file_t conftype)
646 struct ucl_parser *p;
647 ucl_object_t *obj = NULL;
649 p = ucl_parser_new(0);
651 if (!ucl_parser_add_file(p, confpath)) {
653 errx(EXIT_FAILURE, "Unable to parse configuration "
654 "file %s: %s", confpath, ucl_parser_get_error(p));
656 /* no configuration present */
660 obj = ucl_parser_get_object(p);
661 if (obj->type != UCL_OBJECT)
662 warnx("Invalid configuration format, ignoring the "
663 "configuration file %s", confpath);
665 if (conftype == CONFFILE_PKG)
666 config_parse(obj, conftype);
667 else if (conftype == CONFFILE_REPO)
668 parse_repo_file(obj);
671 ucl_object_free(obj);
678 load_repositories(const char *repodir)
684 char path[MAXPATHLEN];
689 if ((d = opendir(repodir)) == NULL)
692 while ((ent = readdir(d))) {
693 /* Trim out 'repos'. */
694 if ((n = strlen(ent->d_name)) <= 5)
696 p = &ent->d_name[n - 5];
697 if (strcmp(p, ".conf") == 0) {
698 snprintf(path, sizeof(path), "%s%s%s",
700 repodir[strlen(repodir) - 1] == '/' ? "" : "/",
702 if (access(path, F_OK) == 0 &&
703 read_conf_file(path, CONFFILE_REPO)) {
721 const char *localbase;
723 char confpath[MAXPATHLEN];
724 struct config_value *cv;
727 for (i = 0; i < CONFIG_SIZE; i++) {
728 val = getenv(c[i].key);
732 case PKG_CONFIG_LIST:
733 /* Split up comma-separated items from env. */
734 c[i].list = malloc(sizeof(*c[i].list));
735 STAILQ_INIT(c[i].list);
736 for (env_list_item = strtok(val, ",");
737 env_list_item != NULL;
738 env_list_item = strtok(NULL, ",")) {
740 malloc(sizeof(struct config_value));
742 strdup(env_list_item);
743 STAILQ_INSERT_TAIL(c[i].list, cv,
754 /* Read LOCALBASE/etc/pkg.conf first. */
755 localbase = getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE;
756 snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf",
759 if (access(confpath, F_OK) == 0 && read_conf_file(confpath,
763 /* Then read in all repos from REPOS_DIR list of directories. */
764 if (c[REPOS_DIR].list == NULL) {
765 c[REPOS_DIR].list = malloc(sizeof(*c[REPOS_DIR].list));
766 STAILQ_INIT(c[REPOS_DIR].list);
767 cv = malloc(sizeof(struct config_value));
768 cv->value = strdup("/etc/pkg");
769 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
770 cv = malloc(sizeof(struct config_value));
771 if (asprintf(&cv->value, "%s/etc/pkg/repos", localbase) < 0)
773 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
776 STAILQ_FOREACH(cv, c[REPOS_DIR].list, next)
777 if (load_repositories(cv->value))
781 if (c[ABI].val == NULL && c[ABI].value == NULL) {
782 if (pkg_get_myabi(abi, BUFSIZ) != 0)
783 errx(EXIT_FAILURE, "Failed to determine the system "
788 subst_packagesite(c[ABI].value != NULL ? c[ABI].value : c[ABI].val);
794 config_string(pkg_config_key k, const char **val)
796 if (c[k].type != PKG_CONFIG_STRING)
799 if (c[k].value != NULL)
808 config_bool(pkg_config_key k, bool *val)
812 if (c[k].type != PKG_CONFIG_BOOL)
817 if (c[k].value != NULL)
822 if (boolstr_to_bool(value))
829 config_finish(void) {
832 for (i = 0; i < CONFIG_SIZE; i++)