1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci
16fb299fa2Sopenharmony_ci#include "partition_utils.h"
17fb299fa2Sopenharmony_ci
18fb299fa2Sopenharmony_ci#include <cstdio>
19fb299fa2Sopenharmony_ci#include <vector>
20fb299fa2Sopenharmony_ci#include <linux/fs.h>
21fb299fa2Sopenharmony_ci#include <sys/ioctl.h>
22fb299fa2Sopenharmony_ci#include <sys/mount.h>
23fb299fa2Sopenharmony_ci#include <sys/types.h>
24fb299fa2Sopenharmony_ci#include <sys/stat.h>
25fb299fa2Sopenharmony_ci#include <fcntl.h>
26fb299fa2Sopenharmony_ci
27fb299fa2Sopenharmony_ci#include "log/log.h"
28fb299fa2Sopenharmony_ci#include "utils.h"
29fb299fa2Sopenharmony_ci
30fb299fa2Sopenharmony_cinamespace Updater {
31fb299fa2Sopenharmony_cinamespace Utils {
32fb299fa2Sopenharmony_ciuint64_t PartitionUtils::GetBlockDeviceSize(int fd) const
33fb299fa2Sopenharmony_ci{
34fb299fa2Sopenharmony_ci    uint64_t size = 0;
35fb299fa2Sopenharmony_ci    return (ioctl(fd, BLKGETSIZE64, &size) == 0) ? size : 0;
36fb299fa2Sopenharmony_ci}
37fb299fa2Sopenharmony_ci
38fb299fa2Sopenharmony_ciint PartitionUtils::WipeBlockPartition() const
39fb299fa2Sopenharmony_ci{
40fb299fa2Sopenharmony_ci    auto fd = open(Updater::Utils::GetPartitionRealPath(devName_).c_str(), O_RDWR);
41fb299fa2Sopenharmony_ci    if (fd < 0) {
42fb299fa2Sopenharmony_ci        LOG(ERROR) << "open partition "<<  devName_.c_str() << " fail, error = " << errno;
43fb299fa2Sopenharmony_ci        return -1;
44fb299fa2Sopenharmony_ci    }
45fb299fa2Sopenharmony_ci
46fb299fa2Sopenharmony_ci    uint64_t size = GetBlockDeviceSize(fd);
47fb299fa2Sopenharmony_ci    uint64_t range[2] = { 0, size };
48fb299fa2Sopenharmony_ci    if (ioctl(fd, BLKSECDISCARD, &range) >= 0) {
49fb299fa2Sopenharmony_ci        close(fd);
50fb299fa2Sopenharmony_ci        return 0;
51fb299fa2Sopenharmony_ci    }
52fb299fa2Sopenharmony_ci
53fb299fa2Sopenharmony_ci    range[0] = 0;
54fb299fa2Sopenharmony_ci    range[1] = size;
55fb299fa2Sopenharmony_ci    if (ioctl(fd, BLKDISCARD, &range) < 0) {
56fb299fa2Sopenharmony_ci        close(fd);
57fb299fa2Sopenharmony_ci        LOG(ERROR) << "BLKDISCARD fail";
58fb299fa2Sopenharmony_ci        return -1;
59fb299fa2Sopenharmony_ci    }
60fb299fa2Sopenharmony_ci    std::vector<uint8_t> buffer(BLOCK_SIZE, 0);
61fb299fa2Sopenharmony_ci    if (!Updater::Utils::WriteFully(fd, buffer.data(), buffer.size())) {
62fb299fa2Sopenharmony_ci        close(fd);
63fb299fa2Sopenharmony_ci        LOG(ERROR) << "wipe block partition write fully fail";
64fb299fa2Sopenharmony_ci        return -1;
65fb299fa2Sopenharmony_ci    }
66fb299fa2Sopenharmony_ci    fsync(fd);
67fb299fa2Sopenharmony_ci    close(fd);
68fb299fa2Sopenharmony_ci    return 0;
69fb299fa2Sopenharmony_ci}
70fb299fa2Sopenharmony_ci}
71fb299fa2Sopenharmony_ci}