1 // Copyright 2018 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 "parent.h"
6 
7 #include <fcntl.h>
8 #include <string.h>
9 
10 #include <zircon/assert.h>
11 #include <zircon/device/device.h>
12 
ParentDevice(const TestConfig & config)13 ParentDevice::ParentDevice(const TestConfig& config) : config_(config) {
14     if (config_.path) {
15         device_.reset(open(config_.path, O_RDWR));
16         path_.Append(config_.path);
17     } else {
18         fuchsia_hardware_nand_RamNandInfo ram_nand_config = {};
19         ram_nand_config.nand_info = config_.info;
20         ram_nand_config.vmo = ZX_HANDLE_INVALID;
21         if (config_.partition_map.partition_count > 0) {
22             ram_nand_config.partition_map = config_.partition_map;
23             ram_nand_config.export_nand_config = true;
24             ram_nand_config.export_partition_map = true;
25         } else {
26             ram_nand_config.export_partition_map = false;
27         }
28         if (fs_mgmt::RamNand::Create(&ram_nand_config, &ram_nand_) == ZX_OK) {
29             path_.Append(ram_nand_->path());
30             config_.num_blocks = config.info.num_blocks;
31         }
32     }
33 }
34 
SetInfo(const fuchsia_hardware_nand_Info & info)35 void ParentDevice::SetInfo(const fuchsia_hardware_nand_Info& info) {
36     ZX_DEBUG_ASSERT(!ram_nand_);
37     config_.info = info;
38     if (!config_.num_blocks) {
39         config_.num_blocks = info.num_blocks;
40     }
41 }
42