2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
16 #include <sys/types.h>
27 #include <klay/audit/dlog-sink.h>
29 #include <upgrade-support.h>
30 #include <internal-encryption-common.h>
31 #include <engine/encryption/dmcrypt-engine.h>
33 audit::LogSink *SINK = nullptr;
37 struct AbstractDevice {
39 virtual ~AbstractDevice() {}
40 virtual std::string findNode(const std::string &name) = 0;
43 struct MmcDevice : public AbstractDevice {
45 MmcDevice(int id) : device("/dev/mmcblk" + std::to_string(id)), deviceHandle(-1)
47 deviceHandle = ::open(device.c_str(), O_RDONLY);
48 if (deviceHandle == -1)
49 throw std::runtime_error("Invalid device: " + device);
54 if (deviceHandle != -1)
55 ::close(deviceHandle);
58 virtual std::string findNode(const std::string &name)
60 blkid_partlist partlist;
64 probe = ::blkid_new_probe();
66 throw std::runtime_error("Failed to call blkid_new_probe");
69 if (::blkid_probe_set_device(probe, deviceHandle, 0, 0) < 0) {
70 ::blkid_free_probe(probe);
71 throw std::runtime_error("Failed to set prove device: " + device);
74 partlist = ::blkid_probe_get_partitions(probe);
76 ::blkid_free_probe(probe);
77 throw std::runtime_error("Failed to get partition list in device: " + device);
80 int num = ::blkid_partlist_numof_partitions(partlist);
81 for (int i = 1; i <= num; i++) {
82 blkid_partition par = ::blkid_partlist_get_partition(partlist, i);
84 const char *n = ::blkid_partition_get_name(par);
88 if (::strcasecmp(n, name.c_str()) == 0) {
89 partno = ::blkid_partition_get_partno(par);
94 ::blkid_free_probe(probe);
97 throw std::runtime_error("Failed to get partition number with " + name);
100 return device + "p" + std::to_string(partno);
108 // dummy implementation
109 ProgressBar::ProgressBar(UpdateFunc const&) : updateValue(0) {}
110 ProgressBar::~ProgressBar() {}
112 void ProgressBar::update(unsigned) {}
113 void ProgressBar::done(void) {}
119 const std::string MOUNT = "mount";
120 const std::string UMOUNT = "umount";
121 const std::string REMOVE = "remove";
126 "Usage: ode-fota [Operation]" << std::endl <<
128 "Operations :" << std::endl <<
129 " mount [path] Mount internal memory using stored master key" << std::endl <<
130 " umount [path] Unmount internal memory" << std::endl <<
131 " remove Remove stored internal memory master key" << std::endl;
134 } // anonymous namespace
136 int main(int argc, char* argv[])
141 if (argc < 2 || argc > 3) {
147 std::string devpath = dev.findNode("user");
149 if (MOUNT == argv[1]) {
150 auto masterKey = UpgradeSupport::loadMasterKey(devpath);
151 std::string path = INTERNAL_PATH;
155 DMCryptEngine dmcrypt(devpath, path, ProgressBar([](unsigned){}));
157 // mount options are ignored by mount()
158 dmcrypt.mount(masterKey, 0);
159 UpgradeSupport::createUpgradeFlag();
160 } else if (UMOUNT == argv[1]) {
161 std::string path = INTERNAL_PATH;
165 DMCryptEngine dmcrypt(devpath, path, ProgressBar([](int){}));
167 } else if (REMOVE == argv[1]) {
168 UpgradeSupport::removeMasterKey(devpath);
174 } catch (const std::exception& e) {
175 std::cerr << e.what() << std::endl;
177 std::cerr << "Unknown exception occured" << std::endl;