1 // Copyright 2016 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 #pragma once
6
7 #include <stdbool.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include <zircon/compiler.h>
12 #include <zircon/types.h>
13
14 __BEGIN_CDECLS
15
16 #define PATH_DATA "/data"
17 #define PATH_INSTALL "/install"
18 #define PATH_SYSTEM "/system"
19 #define PATH_BLOB "/blob"
20 #define PATH_VOLUME "/volume"
21 #define PATH_DEV_BLOCK "/dev/class/block"
22
23 typedef enum disk_format_type {
24 DISK_FORMAT_UNKNOWN,
25 DISK_FORMAT_GPT,
26 DISK_FORMAT_MBR,
27 DISK_FORMAT_MINFS,
28 DISK_FORMAT_FAT,
29 DISK_FORMAT_BLOBFS,
30 DISK_FORMAT_FVM,
31 DISK_FORMAT_ZXCRYPT,
32 DISK_FORMAT_COUNT_,
33 } disk_format_t;
34
35 static const char* disk_format_string_[DISK_FORMAT_COUNT_] = {
36 [DISK_FORMAT_UNKNOWN] = "unknown",
37 [DISK_FORMAT_GPT] = "gpt",
38 [DISK_FORMAT_MBR] = "mbr",
39 [DISK_FORMAT_MINFS] = "minfs",
40 [DISK_FORMAT_FAT] = "fat",
41 [DISK_FORMAT_BLOBFS] = "blobfs",
42 [DISK_FORMAT_FVM] = "fvm",
43 [DISK_FORMAT_ZXCRYPT] = "zxcrypt"};
44
disk_format_string(disk_format_t fs_type)45 static inline const char* disk_format_string(disk_format_t fs_type) {
46 return disk_format_string_[fs_type];
47 }
48
49 #define HEADER_SIZE 4096
50
51 static const uint8_t minfs_magic[16] = {
52 0x21, 0x4d, 0x69, 0x6e, 0x46, 0x53, 0x21, 0x00,
53 0x04, 0xd3, 0xd3, 0xd3, 0xd3, 0x00, 0x50, 0x38,
54 };
55
56 static const uint8_t blobfs_magic[16] = {
57 0x21, 0x4d, 0x69, 0x9e, 0x47, 0x53, 0x21, 0xac,
58 0x14, 0xd3, 0xd3, 0xd4, 0xd4, 0x00, 0x50, 0x98,
59 };
60
61 static const uint8_t gpt_magic[16] = {
62 0x45, 0x46, 0x49, 0x20, 0x50, 0x41, 0x52, 0x54,
63 0x00, 0x00, 0x01, 0x00, 0x5c, 0x00, 0x00, 0x00,
64 };
65
66 static const uint8_t fvm_magic[8] = {
67 0x46, 0x56, 0x4d, 0x20, 0x50, 0x41, 0x52, 0x54,
68 };
69
70 static const uint8_t zxcrypt_magic[16] = {
71 0x5f, 0xe8, 0xf8, 0x00, 0xb3, 0x6d, 0x11, 0xe7,
72 0x80, 0x7a, 0x78, 0x63, 0x72, 0x79, 0x70, 0x74,
73 };
74
75 disk_format_t detect_disk_format(int fd);
76
77 typedef struct mount_options {
78 bool readonly;
79 bool verbose_mount;
80 bool collect_metrics;
81 // Ensures that requests to the mountpoint will be propagated to the underlying FS
82 bool wait_until_ready;
83 // Create the mountpoint directory if it doesn't already exist.
84 // Must be false if passed to "fmount".
85 bool create_mountpoint;
86 // Enable journaling on the file system (if supported).
87 bool enable_journal;
88 } mount_options_t;
89
90 extern const mount_options_t default_mount_options;
91
92 typedef struct mkfs_options {
93 uint32_t fvm_data_slices;
94 bool verbose;
95 } mkfs_options_t;
96
97 extern const mkfs_options_t default_mkfs_options;
98
99 #define NUM_MKFS_OPTIONS 2
100
101 typedef struct fsck_options {
102 bool verbose;
103 // At MOST one of the following '*_modify' flags may be true.
104 bool never_modify; // Fsck still looks for problems, but it does not try to resolve them.
105 bool always_modify; // Fsck never asks to resolve problems; it assumes it should fix them.
106 bool force; // Force fsck to check the filesystem integrity, even if it is marked as "clean".
107 } fsck_options_t;
108
109 #define NUM_FSCK_OPTIONS 3
110
111 extern const fsck_options_t default_fsck_options;
112
113 typedef zx_status_t (*LaunchCallback)(int argc, const char** argv,
114 zx_handle_t* hnd, uint32_t* ids, size_t len);
115
116 // Creates no logs, waits for process to terminate.
117 zx_status_t launch_silent_sync(int argc, const char** argv, zx_handle_t* handles,
118 uint32_t* types, size_t len);
119 // Creates no logs, does not wait for process to terminate.
120 zx_status_t launch_silent_async(int argc, const char** argv, zx_handle_t* handles,
121 uint32_t* types, size_t len);
122 // Creates stdio logs, waits for process to terminate.
123 zx_status_t launch_stdio_sync(int argc, const char** argv, zx_handle_t* handles,
124 uint32_t* types, size_t len);
125 // Creates stdio logs, does not wait for process to terminate.
126 zx_status_t launch_stdio_async(int argc, const char** argv, zx_handle_t* handles,
127 uint32_t* types, size_t len);
128 // Creates kernel logs, does not wait for process to terminate.
129 zx_status_t launch_logs_async(int argc, const char** argv, zx_handle_t* handles,
130 uint32_t* types, size_t len);
131
132 // Given the following:
133 // - A device containing a filesystem image of a known format
134 // - A path on which to mount the filesystem
135 // - Some configuration options for launching the filesystem, and
136 // - A callback which can be used to 'launch' an fs server,
137 //
138 // Prepare the argv arguments to the filesystem process, mount a handle on the
139 // expected mount_path, and call the 'launch' callback (if the filesystem is
140 // recognized).
141 //
142 // device_fd is always consumed. If the callback is reached, then the 'device_fd'
143 // is transferred via handles to the callback arguments.
144 zx_status_t mount(int device_fd, const char* mount_path, disk_format_t df,
145 const mount_options_t* options, LaunchCallback cb);
146 // 'mount_fd' is used in lieu of the mount_path. It is not consumed (i.e.,
147 // it will still be open after this function completes, regardless of
148 // success or failure).
149 zx_status_t fmount(int device_fd, int mount_fd, disk_format_t df,
150 const mount_options_t* options, LaunchCallback cb);
151
152 // Format the provided device with a requested disk format.
153 zx_status_t mkfs(const char* device_path, disk_format_t df, LaunchCallback cb,
154 const mkfs_options_t* options);
155
156 // Check and repair a device with a requested disk format.
157 zx_status_t fsck(const char* device_path, disk_format_t df,
158 const fsck_options_t* options, LaunchCallback cb);
159
160 // Umount the filesystem process.
161 //
162 // Returns ZX_ERR_BAD_STATE if mount_path could not be opened.
163 // Returns ZX_ERR_NOT_FOUND if there is no mounted filesystem on mount_path.
164 // Other errors may also be returned if problems occur while unmounting.
165 zx_status_t umount(const char* mount_path);
166 // 'mount_fd' is used in lieu of the mount_path. It is not consumed.
167 zx_status_t fumount(int mount_fd);
168
169 __END_CDECLS
170