Skip to content

Commit f72949b

Browse files
gilhooleydCQ Bot
authored andcommitted
[ramdevice_client] Separate IsolatedDevmgr code
Pull the IsolatedDevmgr code out into a ramdevice_client_test library. This enables us to make the IsolatedDevmgr code testonly. Bug: 84322 Change-Id: I96a846d2834da03b9238210d914a993bd8c5a89e Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/579541 Reviewed-by: Suraj Malhotra <surajmalhotra@google.com> Commit-Queue: David Gilhooley <dgilhooley@google.com>
1 parent f104735 commit f72949b

File tree

5 files changed

+203
-107
lines changed

5 files changed

+203
-107
lines changed

src/lib/storage/ramdevice_client/cpp/BUILD.gn

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,37 @@ shared_library("ramdevice-client") {
1717
"ramnand.cc",
1818
]
1919
public_deps = [
20-
# <ramdevice-client/ramnand.h> has #include <lib/driver-integration-test/fixture.h>
21-
"//zircon/system/ulib/driver-integration-test:driver-integration-test-static",
20+
# <ramdevice-client/ramnand.h> has #include <fuchsia/hardware/nand/c/fidl.h>
21+
"//sdk/fidl/fuchsia.hardware.nand:fuchsia.hardware.nand_c",
22+
]
23+
deps = [
24+
"//sdk/fidl/fuchsia.device:fuchsia.device_c",
25+
"//sdk/fidl/fuchsia.device:fuchsia.device_llcpp",
26+
"//sdk/fidl/fuchsia.hardware.block:fuchsia.hardware.block_c",
27+
"//sdk/fidl/fuchsia.hardware.nand:fuchsia.hardware.nand_c",
28+
"//sdk/fidl/fuchsia.hardware.ramdisk:fuchsia.hardware.ramdisk_c",
29+
"//sdk/lib/fdio",
30+
"//src/lib/ddk",
31+
"//zircon/public/lib/fbl",
32+
"//zircon/public/lib/zx",
33+
"//zircon/system/ulib/fdio-caller",
34+
"//zircon/system/ulib/fzl",
35+
]
36+
configs += [ "//build/config/fuchsia:static_cpp_standard_library" ]
37+
}
38+
39+
shared_library("ramdevice-client-test") {
40+
testonly = true
41+
public_configs = [ ":headers_config" ]
42+
public = [ "include/ramdevice-client-test/ramnandctl.h" ]
43+
sources = [ "ramnandctl.cc" ]
44+
public_deps = [
45+
# <ramdevice-client-test/ramnandctl.h> has #include <lib/driver-integration-test/fixture.h>
46+
":ramdevice-client",
2247

2348
# <ramdevice-client/ramnand.h> has #include <fuchsia/hardware/nand/c/fidl.h>
2449
"//sdk/fidl/fuchsia.hardware.nand:fuchsia.hardware.nand_c",
50+
"//zircon/system/ulib/driver-integration-test:driver-integration-test-static",
2551
]
2652
deps = [
2753
"//sdk/fidl/fuchsia.device:fuchsia.device_c",
@@ -51,17 +77,43 @@ static_library("ramdevice-client-static") {
5177
"ramdisk.cc",
5278
"ramnand.cc",
5379
]
80+
public_deps = [
81+
# <ramdevice-client/ramnand.h> has #include <fuchsia/hardware/nand/c/fidl.h>
82+
"//sdk/fidl/fuchsia.hardware.nand:fuchsia.hardware.nand_c",
83+
]
84+
deps = [
85+
"//sdk/fidl/fuchsia.device:fuchsia.device_c",
86+
"//sdk/fidl/fuchsia.device:fuchsia.device_llcpp",
87+
"//sdk/fidl/fuchsia.hardware.block:fuchsia.hardware.block_c",
88+
"//sdk/fidl/fuchsia.hardware.ramdisk:fuchsia.hardware.ramdisk_c",
89+
"//sdk/lib/fdio",
90+
"//src/lib/ddk",
91+
"//zircon/public/lib/fbl",
92+
"//zircon/public/lib/zx",
93+
"//zircon/system/ulib/fdio-caller",
94+
"//zircon/system/ulib/fzl",
95+
]
96+
configs += [ "//build/config/fuchsia:static_cpp_standard_library" ]
97+
}
98+
99+
static_library("ramdevice-client-test-static") {
100+
testonly = true
101+
public_configs = [ ":headers_config" ]
102+
public = [ "include/ramdevice-client-test/ramnandctl.h" ]
103+
sources = [ "ramnandctl.cc" ]
54104
public_deps = [
55105
# <ramdevice-client/ramnand.h> has #include <lib/driver-integration-test/fixture.h>
56106
"//zircon/system/ulib/driver-integration-test:driver-integration-test-static",
57107

58108
# <ramdevice-client/ramnand.h> has #include <fuchsia/hardware/nand/c/fidl.h>
109+
":ramdevice-client",
59110
"//sdk/fidl/fuchsia.hardware.nand:fuchsia.hardware.nand_c",
60111
]
61112
deps = [
62113
"//sdk/fidl/fuchsia.device:fuchsia.device_c",
63114
"//sdk/fidl/fuchsia.device:fuchsia.device_llcpp",
64115
"//sdk/fidl/fuchsia.hardware.block:fuchsia.hardware.block_c",
116+
"//sdk/fidl/fuchsia.hardware.nand:fuchsia.hardware.nand_c",
65117
"//sdk/fidl/fuchsia.hardware.ramdisk:fuchsia.hardware.ramdisk_c",
66118
"//sdk/lib/fdio",
67119
"//src/lib/ddk",
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2021 The Fuchsia Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
#ifndef SRC_LIB_STORAGE_RAMDEVICE_CLIENT_CPP_INCLUDE_RAMDEVICE_CLIENT_TEST_RAMNANDCTL_H_
5+
#define SRC_LIB_STORAGE_RAMDEVICE_CLIENT_CPP_INCLUDE_RAMDEVICE_CLIENT_TEST_RAMNANDCTL_H_
6+
7+
#include <lib/driver-integration-test/fixture.h>
8+
9+
#include "src/lib/storage/ramdevice_client/cpp/include/ramdevice-client/ramnand.h"
10+
11+
namespace ramdevice_client_test {
12+
13+
using ramdevice_client::RamNand;
14+
15+
class RamNandCtl : public fbl::RefCounted<RamNandCtl> {
16+
public:
17+
// Creates an isolated devmgr and spawns a ram_nand_ctl device in it.
18+
static zx_status_t Create(fbl::RefPtr<RamNandCtl>* out);
19+
20+
static zx_status_t CreateWithRamNand(const fuchsia_hardware_nand_RamNandInfo* config,
21+
std::optional<RamNand>* out);
22+
23+
zx_status_t CreateRamNand(const fuchsia_hardware_nand_RamNandInfo* config,
24+
std::optional<RamNand>* out);
25+
26+
~RamNandCtl() = default;
27+
28+
const fbl::unique_fd& fd() { return ctl_; }
29+
const fbl::unique_fd& devfs_root() { return devmgr_.devfs_root(); }
30+
31+
private:
32+
RamNandCtl(driver_integration_test::IsolatedDevmgr devmgr, fbl::unique_fd ctl)
33+
: devmgr_(std::move(devmgr)), ctl_(std::move(ctl)) {}
34+
35+
driver_integration_test::IsolatedDevmgr devmgr_;
36+
fbl::unique_fd ctl_;
37+
};
38+
39+
} // namespace ramdevice_client_test
40+
41+
#endif // SRC_LIB_STORAGE_RAMDEVICE_CLIENT_CPP_INCLUDE_RAMDEVICE_CLIENT_TEST_RAMNANDCTL_H_

src/lib/storage/ramdevice_client/cpp/include/ramdevice-client/ramnand.h

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include <fuchsia/hardware/nand/c/fidl.h>
99
#include <inttypes.h>
10-
#include <lib/driver-integration-test/fixture.h>
10+
#include <lib/zx/channel.h>
1111
#include <zircon/compiler.h>
1212

1313
#include <memory>
@@ -19,32 +19,6 @@
1919

2020
namespace ramdevice_client {
2121

22-
class RamNand;
23-
24-
class RamNandCtl : public fbl::RefCounted<RamNandCtl> {
25-
public:
26-
// Creates an isolated devmgr and spawns a ram_nand_ctl device in it.
27-
static zx_status_t Create(fbl::RefPtr<RamNandCtl>* out);
28-
29-
static zx_status_t CreateWithRamNand(const fuchsia_hardware_nand_RamNandInfo* config,
30-
std::optional<RamNand>* out);
31-
32-
zx_status_t CreateRamNand(const fuchsia_hardware_nand_RamNandInfo* config,
33-
std::optional<RamNand>* out);
34-
35-
~RamNandCtl() = default;
36-
37-
const fbl::unique_fd& fd() { return ctl_; }
38-
const fbl::unique_fd& devfs_root() { return devmgr_.devfs_root(); }
39-
40-
private:
41-
RamNandCtl(driver_integration_test::IsolatedDevmgr devmgr, fbl::unique_fd ctl)
42-
: devmgr_(std::move(devmgr)), ctl_(std::move(ctl)) {}
43-
44-
driver_integration_test::IsolatedDevmgr devmgr_;
45-
fbl::unique_fd ctl_;
46-
};
47-
4822
class RamNand {
4923
public:
5024
static constexpr char kBasePath[] = "/dev/sys/platform/00:00:2e/nand-ctl";

src/lib/storage/ramdevice_client/cpp/ramnand.cc

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -56,84 +56,6 @@ zx_status_t WaitForFile(const fbl::unique_fd& dir, const char* file, fbl::unique
5656

5757
namespace ramdevice_client {
5858

59-
__EXPORT
60-
zx_status_t RamNandCtl::Create(fbl::RefPtr<RamNandCtl>* out) {
61-
driver_integration_test::IsolatedDevmgr::Args args;
62-
args.disable_block_watcher = true;
63-
// TODO(surajmalhotra): Remove creation of isolated devmgr from this lib so that caller can choose
64-
// their creation parameters.
65-
args.board_name = "astro";
66-
67-
driver_integration_test::IsolatedDevmgr devmgr;
68-
zx_status_t st = driver_integration_test::IsolatedDevmgr::Create(&args, &devmgr);
69-
if (st != ZX_OK) {
70-
fprintf(stderr, "Could not create ram_nand_ctl device, %d\n", st);
71-
return st;
72-
}
73-
74-
fbl::unique_fd ctl;
75-
st = devmgr_integration_test::RecursiveWaitForFile(devmgr.devfs_root(),
76-
"sys/platform/00:00:2e/nand-ctl", &ctl);
77-
if (st != ZX_OK) {
78-
fprintf(stderr, "ram_nand_ctl device failed enumerated, %d\n", st);
79-
return st;
80-
}
81-
82-
*out = fbl::AdoptRef(new RamNandCtl(std::move(devmgr), std::move(ctl)));
83-
return ZX_OK;
84-
}
85-
86-
__EXPORT
87-
zx_status_t RamNandCtl::CreateRamNand(const fuchsia_hardware_nand_RamNandInfo* config,
88-
std::optional<RamNand>* out) {
89-
fdio_t* io = fdio_unsafe_fd_to_io(fd().get());
90-
if (io == NULL) {
91-
fprintf(stderr, "Could not get fdio object\n");
92-
return ZX_ERR_INTERNAL;
93-
}
94-
zx_handle_t ctl_svc = fdio_unsafe_borrow_channel(io);
95-
96-
char name[fuchsia_hardware_nand_NAME_LEN + 1];
97-
size_t out_name_size;
98-
zx_status_t status;
99-
zx_status_t st = fuchsia_hardware_nand_RamNandCtlCreateDevice(
100-
ctl_svc, config, &status, name, fuchsia_hardware_nand_NAME_LEN, &out_name_size);
101-
fdio_unsafe_release(io);
102-
if (st != ZX_OK || status != ZX_OK) {
103-
st = st != ZX_OK ? st : status;
104-
fprintf(stderr, "Could not create ram_nand device, %d\n", st);
105-
return st;
106-
}
107-
name[out_name_size] = '\0';
108-
109-
// TODO(fxbug.dev/33003): We should be able to open relative to ctl->fd(), but
110-
// due to a bug, we have to be relative to devfs_root instead.
111-
fbl::StringBuffer<PATH_MAX> path;
112-
path.Append("sys/platform/00:00:2e/nand-ctl/");
113-
path.Append(name);
114-
fprintf(stderr, "Trying to open (%s)\n", path.c_str());
115-
116-
fbl::unique_fd fd;
117-
st = devmgr_integration_test::RecursiveWaitForFile(devfs_root(), path.c_str(), &fd);
118-
if (st != ZX_OK) {
119-
return st;
120-
}
121-
122-
*out = RamNand(std::move(fd));
123-
return ZX_OK;
124-
}
125-
126-
__EXPORT
127-
zx_status_t RamNandCtl::CreateWithRamNand(const fuchsia_hardware_nand_RamNandInfo* config,
128-
std::optional<RamNand>* out) {
129-
fbl::RefPtr<RamNandCtl> ctl;
130-
zx_status_t st = RamNandCtl::Create(&ctl);
131-
if (st != ZX_OK) {
132-
return st;
133-
}
134-
return ctl->CreateRamNand(config, out);
135-
}
136-
13759
__EXPORT
13860
zx_status_t RamNand::Create(const fuchsia_hardware_nand_RamNandInfo* config,
13961
std::optional<RamNand>* out) {
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright 2021 The Fuchsia Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include <fcntl.h>
6+
#include <fidl/fuchsia.device/cpp/wire.h>
7+
#include <fuchsia/device/c/fidl.h>
8+
#include <fuchsia/hardware/nand/c/fidl.h>
9+
#include <lib/fdio/cpp/caller.h>
10+
#include <lib/fdio/directory.h>
11+
#include <lib/fdio/fd.h>
12+
#include <lib/fdio/fdio.h>
13+
#include <lib/fdio/unsafe.h>
14+
#include <lib/fdio/watcher.h>
15+
#include <limits.h>
16+
#include <stdio.h>
17+
#include <stdlib.h>
18+
#include <sys/stat.h>
19+
#include <zircon/types.h>
20+
21+
#include <utility>
22+
23+
#include <fbl/string_buffer.h>
24+
#include <fbl/unique_fd.h>
25+
#include <ramdevice-client-test/ramnandctl.h>
26+
27+
namespace ramdevice_client_test {
28+
29+
__EXPORT
30+
zx_status_t RamNandCtl::Create(fbl::RefPtr<RamNandCtl>* out) {
31+
driver_integration_test::IsolatedDevmgr::Args args;
32+
args.disable_block_watcher = true;
33+
// TODO(surajmalhotra): Remove creation of isolated devmgr from this lib so that caller can choose
34+
// their creation parameters.
35+
args.board_name = "astro";
36+
37+
driver_integration_test::IsolatedDevmgr devmgr;
38+
zx_status_t st = driver_integration_test::IsolatedDevmgr::Create(&args, &devmgr);
39+
if (st != ZX_OK) {
40+
fprintf(stderr, "Could not create ram_nand_ctl device, %d\n", st);
41+
return st;
42+
}
43+
44+
fbl::unique_fd ctl;
45+
st = devmgr_integration_test::RecursiveWaitForFile(devmgr.devfs_root(),
46+
"sys/platform/00:00:2e/nand-ctl", &ctl);
47+
if (st != ZX_OK) {
48+
fprintf(stderr, "ram_nand_ctl device failed enumerated, %d\n", st);
49+
return st;
50+
}
51+
52+
*out = fbl::AdoptRef(new RamNandCtl(std::move(devmgr), std::move(ctl)));
53+
return ZX_OK;
54+
}
55+
56+
__EXPORT
57+
zx_status_t RamNandCtl::CreateRamNand(const fuchsia_hardware_nand_RamNandInfo* config,
58+
std::optional<RamNand>* out) {
59+
fdio_t* io = fdio_unsafe_fd_to_io(fd().get());
60+
if (io == NULL) {
61+
fprintf(stderr, "Could not get fdio object\n");
62+
return ZX_ERR_INTERNAL;
63+
}
64+
zx_handle_t ctl_svc = fdio_unsafe_borrow_channel(io);
65+
66+
char name[fuchsia_hardware_nand_NAME_LEN + 1];
67+
size_t out_name_size;
68+
zx_status_t status;
69+
zx_status_t st = fuchsia_hardware_nand_RamNandCtlCreateDevice(
70+
ctl_svc, config, &status, name, fuchsia_hardware_nand_NAME_LEN, &out_name_size);
71+
fdio_unsafe_release(io);
72+
if (st != ZX_OK || status != ZX_OK) {
73+
st = st != ZX_OK ? st : status;
74+
fprintf(stderr, "Could not create ram_nand device, %d\n", st);
75+
return st;
76+
}
77+
name[out_name_size] = '\0';
78+
79+
// TODO(fxbug.dev/33003): We should be able to open relative to ctl->fd(), but
80+
// due to a bug, we have to be relative to devfs_root instead.
81+
fbl::StringBuffer<PATH_MAX> path;
82+
path.Append("sys/platform/00:00:2e/nand-ctl/");
83+
path.Append(name);
84+
fprintf(stderr, "Trying to open (%s)\n", path.c_str());
85+
86+
fbl::unique_fd fd;
87+
st = devmgr_integration_test::RecursiveWaitForFile(devfs_root(), path.c_str(), &fd);
88+
if (st != ZX_OK) {
89+
return st;
90+
}
91+
92+
*out = RamNand(std::move(fd));
93+
return ZX_OK;
94+
}
95+
96+
__EXPORT
97+
zx_status_t RamNandCtl::CreateWithRamNand(const fuchsia_hardware_nand_RamNandInfo* config,
98+
std::optional<RamNand>* out) {
99+
fbl::RefPtr<RamNandCtl> ctl;
100+
zx_status_t st = RamNandCtl::Create(&ctl);
101+
if (st != ZX_OK) {
102+
return st;
103+
}
104+
return ctl->CreateRamNand(config, out);
105+
}
106+
107+
} // namespace ramdevice_client_test

0 commit comments

Comments
 (0)