Coverage Report

Created: 2026-11-13 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/common/binfmt.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
 * Copyright (C) 2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
4
 */
5
6
#include <common.h>
7
#include <binfmt.h>
8
#include <libbb.h>
9
#include <malloc.h>
10
#include <command.h>
11
#include <errno.h>
12
13
static LIST_HEAD(binfmt_hooks);
14
15
static int binfmt_run(char *file, int argc, char **argv)
16
0
{
17
0
  struct binfmt_hook *b;
18
0
  enum filetype type;
19
0
  int ret;
20
21
0
  ret = file_name_detect_type(file, &type);
22
0
  if (ret)
23
0
    return ret;
24
25
0
  list_for_each_entry(b, &binfmt_hooks, list) {
26
0
    if (b->type != type)
27
0
      continue;
28
29
0
    ret = b->hook(b, file, argc, argv);
30
0
    if (ret != -ERESTARTNOHAND)
31
0
      return ret;
32
0
  }
33
0
  return -ENOIOCTLCMD;
34
0
}
35
36
/*
37
 * This converts the original '/executable <args>' into
38
 * 'barebox_cmd <args> /executable'
39
 */
40
static int binfmt_exec_excute(struct binfmt_hook *b, char *file, int argc, char **argv)
41
0
{
42
0
  char **newargv = xzalloc(sizeof(char*) * (argc + 1));
43
0
  int ret, i;
44
45
0
  newargv[0] = b->exec;
46
47
0
  for (i = 1 ; i < argc; i++)
48
0
    newargv[i] = argv[i];
49
0
  newargv[i] = file;
50
51
0
  ret = execute_binfmt(argc + 1, newargv);
52
53
0
  free(newargv);
54
55
0
  return ret;
56
0
}
57
58
int execute_binfmt(int argc, char **argv)
59
0
{
60
0
  int ret;
61
0
  char *path;
62
63
0
  if (strchr(argv[0], '/'))
64
0
    return binfmt_run(argv[0], argc, argv);
65
66
0
  if (find_cmd(argv[0]))
67
0
    return execute_command(argc, &argv[0]);
68
69
0
  path = find_execable(argv[0]);
70
0
  if (path) {
71
0
    ret = binfmt_run(path, argc, argv);
72
0
    free(path);
73
0
    return ret;
74
0
  }
75
76
0
  return -ENOIOCTLCMD;
77
0
}
78
79
int binfmt_register(struct binfmt_hook *b)
80
60
{
81
60
  if (!b || !b->type)
82
0
    return -EIO;
83
84
60
  if (!b->hook && !b->exec)
85
0
    return -EIO;
86
87
60
  if (b->exec)
88
30
    b->hook = binfmt_exec_excute;
89
90
60
  list_add_tail(&b->list, &binfmt_hooks);
91
92
60
  return 0;
93
60
}
94
95
void binfmt_unregister(struct binfmt_hook *b)
96
0
{
97
0
  if (!b)
98
0
    return;
99
100
0
  list_del(&b->list);
101
0
}