Coverage Report

Created: 2026-03-11 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/u-boot/cmd/host.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0+
2
/*
3
 * Copyright (c) 2012, Google Inc.
4
 */
5
6
#include <command.h>
7
#include <dm.h>
8
#include <fs.h>
9
#include <part.h>
10
#include <sandbox_host.h>
11
#include <dm/device_compat.h>
12
#include <dm/device-internal.h>
13
#include <dm/uclass-internal.h>
14
#include <linux/errno.h>
15
#include <linux/log2.h>
16
17
static int do_host_load(struct cmd_tbl *cmdtp, int flag, int argc,
18
      char *const argv[])
19
0
{
20
0
  return do_load(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
21
0
}
22
23
static int do_host_ls(struct cmd_tbl *cmdtp, int flag, int argc,
24
          char *const argv[])
25
0
{
26
0
  return do_ls(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
27
0
}
28
29
static int do_host_size(struct cmd_tbl *cmdtp, int flag, int argc,
30
      char *const argv[])
31
0
{
32
0
  return do_size(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
33
0
}
34
35
static int do_host_save(struct cmd_tbl *cmdtp, int flag, int argc,
36
      char *const argv[])
37
0
{
38
0
  return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
39
0
}
40
41
static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc,
42
      char *const argv[])
43
0
{
44
0
  bool removable = false;
45
0
  struct udevice *dev;
46
0
  const char *label;
47
0
  char *file;
48
0
  unsigned long blksz = DEFAULT_BLKSZ;
49
0
  int ret;
50
51
  /* Skip 'bind' */
52
0
  argc--;
53
0
  argv++;
54
0
  if (argc < 2)
55
0
    return CMD_RET_USAGE;
56
57
0
  if (!strcmp(argv[0], "-r")) {
58
0
    removable = true;
59
0
    argc--;
60
0
    argv++;
61
0
  }
62
63
0
  if (argc < 2 || argc > 3)
64
0
    return CMD_RET_USAGE;
65
0
  label = argv[0];
66
0
  file = argv[1];
67
0
  if (argc > 2) {
68
0
    blksz = dectoul(argv[2], NULL);
69
0
    if (blksz < DEFAULT_BLKSZ || !is_power_of_2(blksz)) {
70
0
      printf("blksz must be >= 512 and power of 2\n");
71
0
      return CMD_RET_FAILURE;
72
0
    }
73
0
  }
74
75
0
  ret = host_create_attach_file(label, file, removable, blksz, &dev);
76
0
  if (ret) {
77
0
    printf("Cannot create device / bind file\n");
78
0
    return CMD_RET_FAILURE;
79
0
  }
80
81
0
  return 0;
82
0
}
83
84
/**
85
 * parse_host_label() - Parse a device label or sequence number
86
 *
87
 * This shows an error if it returns NULL
88
 *
89
 * @label: String containing the label or sequence number
90
 * Returns: Associated device, or NULL if not found
91
 */
92
static struct udevice *parse_host_label(const char *label)
93
0
{
94
0
  struct udevice *dev;
95
96
0
  dev = host_find_by_label(label);
97
0
  if (!dev) {
98
0
    int devnum;
99
0
    char *ep;
100
101
0
    devnum = hextoul(label, &ep);
102
0
    if (*ep ||
103
0
        uclass_find_device_by_seq(UCLASS_HOST, devnum, &dev)) {
104
0
      printf("No such device '%s'\n", label);
105
0
      return NULL;
106
0
    }
107
0
  }
108
109
0
  return dev;
110
0
}
111
112
static int do_host_unbind(struct cmd_tbl *cmdtp, int flag, int argc,
113
        char *const argv[])
114
0
{
115
0
  struct udevice *dev;
116
0
  const char *label;
117
0
  int ret;
118
119
0
  if (argc < 2)
120
0
    return CMD_RET_USAGE;
121
122
0
  label = argv[1];
123
0
  dev = parse_host_label(label);
124
0
  if (!dev)
125
0
    return CMD_RET_FAILURE;
126
127
0
  ret = host_detach_file(dev);
128
0
  if (ret) {
129
0
    printf("Cannot detach file (err=%d)\n", ret);
130
0
    return CMD_RET_FAILURE;
131
0
  }
132
133
0
  ret = device_unbind(dev);
134
0
  if (ret) {
135
0
    printf("Cannot attach file\n");
136
0
    ret = device_unbind(dev);
137
0
    if (ret)
138
0
      printf("Cannot unbind device '%s'\n", dev->name);
139
0
    return CMD_RET_FAILURE;
140
0
  }
141
142
0
  return 0;
143
0
}
144
145
static void show_host_dev(struct udevice *dev)
146
0
{
147
0
  struct host_sb_plat *plat = dev_get_plat(dev);
148
0
  struct blk_desc *desc;
149
0
  struct udevice *blk;
150
0
  int ret;
151
152
0
  printf("%3d ", dev_seq(dev));
153
0
  if (!plat->fd) {
154
0
    printf("Not bound to a backing file\n");
155
0
    return;
156
0
  }
157
0
  ret = blk_get_from_parent(dev, &blk);
158
0
  if (ret)  /* cannot happen */
159
0
    return;
160
161
0
  desc = dev_get_uclass_plat(blk);
162
0
  printf("%12lu %6lu %-15s %s\n", (unsigned long)desc->lba, desc->blksz,
163
0
         plat->label, plat->filename);
164
0
}
165
166
static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc,
167
      char *const argv[])
168
0
{
169
0
  struct udevice *dev;
170
171
0
  if (argc < 1)
172
0
    return CMD_RET_USAGE;
173
174
0
  dev = NULL;
175
0
  if (argc >= 2) {
176
0
    dev = parse_host_label(argv[1]);
177
0
    if (!dev)
178
0
      return CMD_RET_FAILURE;
179
0
  }
180
181
0
  printf("%3s %12s %6s %-15s %s\n",
182
0
         "dev", "blocks", "blksz", "label", "path");
183
0
  if (dev) {
184
0
    show_host_dev(dev);
185
0
  } else {
186
0
    struct uclass *uc;
187
188
0
    uclass_id_foreach_dev(UCLASS_HOST, dev, uc)
189
0
      show_host_dev(dev);
190
0
  }
191
192
0
  return 0;
193
0
}
194
195
static int do_host_dev(struct cmd_tbl *cmdtp, int flag, int argc,
196
           char *const argv[])
197
0
{
198
0
  struct udevice *dev;
199
0
  const char *label;
200
201
0
  if (argc < 1 || argc > 3)
202
0
    return CMD_RET_USAGE;
203
204
0
  if (argc == 1) {
205
0
    struct host_sb_plat *plat;
206
207
0
    dev = host_get_cur_dev();
208
0
    if (!dev) {
209
0
      printf("No current host device\n");
210
0
      return CMD_RET_FAILURE;
211
0
    }
212
0
    plat = dev_get_plat(dev);
213
0
    printf("Current host device: %d: %s\n", dev_seq(dev),
214
0
           plat->label);
215
0
    return 0;
216
0
  }
217
218
0
  label = argv[1];
219
0
  dev = parse_host_label(argv[1]);
220
0
  if (!dev)
221
0
    return CMD_RET_FAILURE;
222
223
0
  host_set_cur_dev(dev);
224
225
0
  return 0;
226
0
}
227
228
static struct cmd_tbl cmd_host_sub[] = {
229
  U_BOOT_CMD_MKENT(load, 7, 0, do_host_load, "", ""),
230
  U_BOOT_CMD_MKENT(ls, 3, 0, do_host_ls, "", ""),
231
  U_BOOT_CMD_MKENT(save, 6, 0, do_host_save, "", ""),
232
  U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
233
  U_BOOT_CMD_MKENT(bind, 4, 0, do_host_bind, "", ""),
234
  U_BOOT_CMD_MKENT(unbind, 4, 0, do_host_unbind, "", ""),
235
  U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
236
  U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
237
};
238
239
static int do_host(struct cmd_tbl *cmdtp, int flag, int argc,
240
       char *const argv[])
241
0
{
242
0
  struct cmd_tbl *c;
243
244
  /* Skip past 'host' */
245
0
  argc--;
246
0
  argv++;
247
248
0
  c = find_cmd_tbl(argv[0], cmd_host_sub, ARRAY_SIZE(cmd_host_sub));
249
250
0
  if (c)
251
0
    return c->cmd(cmdtp, flag, argc, argv);
252
0
  else
253
0
    return CMD_RET_USAGE;
254
0
}
255
256
U_BOOT_CMD(
257
  host, 8, 1, do_host,
258
  "Miscellaneous host commands",
259
  "load hostfs - <addr> <filename> [<bytes> <offset>]  - "
260
    "load a file from host\n"
261
  "host ls hostfs - <filename>                    - list files on host\n"
262
  "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
263
    "save a file to host\n"
264
  "host size hostfs - <filename> - determine size of file on host\n"
265
  "host bind [-r] <label> <filename> [<blksz>] - bind \"host\" device to file,\n"
266
  "     and optionally set the device's logical block size\n"
267
  "     -r = mark as removable\n"
268
  "host unbind <label>     - unbind file from \"host\" device\n"
269
  "host info [<label>]     - show device binding & info\n"
270
  "host dev [<label>]      - set or retrieve the current host device\n"
271
  "host commands use the \"hostfs\" device. The \"host\" device is used\n"
272
  "with standard IO commands such as fatls or ext2load"
273
);