misc_daemon.cpp 6.9 KB
Newer Older
S
sun_fan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cerrno>
#include <cstdint>
#include <fcntl.h>
#include <getopt.h>
#include <iostream>
#include <string>
#include <sys/stat.h>
#include "fs_manager/fs_manager.h"
#include "misc_info.h"
#include "param_wrapper.h"

constexpr int PARTITION_INFO_POS = 1144;
constexpr int PARTITION_INFO_MAX_LENGTH = 256;
constexpr int BLOCK_SZIE_1 = 512;
constexpr uint64_t LOGO_MAGIC = 0XABCABCAB;

struct option g_options[] = {
    { "write_logo", required_argument, nullptr, 0 },
    { nullptr, 0, nullptr, 0 },
};

static std::string GetMiscDevicePath()
{
    std::string miscDev {};
    // Get misc device path from fstab
    std::string hardwareVal {};
    int ret = OHOS::system::GetStringParameter("ohos.boot.hardware", hardwareVal, "");
    if (ret != 0) {
        std::cout << "get ohos.boot.hardware failed\n";
        return "";
    }
    std::string fstabFileName = std::string("fstab.") + hardwareVal;
    std::string fstabFile = std::string("/vendor/etc/") + fstabFileName;
    Fstab *fstab = ReadFstabFromFile(fstabFile.c_str(), false);
    if (fstab == nullptr) {
        std::cout << "Failed to read fstab\n";
        return miscDev;
    }

    FstabItem *misc = FindFstabItemForMountPoint(*fstab, "/misc");
    if (misc == nullptr) {
        std::cout << "Cannot find misc partition from fstab\n";
        return miscDev;
    }
    miscDev = misc->deviceName;
    ReleaseFstab(fstab);
    return miscDev;
}


static void ClearLogo(int fd)
{
    if (fd < 0) {
        return;
    }
    char buffer[8] = {0}; // logo magic number + logo size is 8
    int addrOffset = (PARTITION_INFO_POS + PARTITION_INFO_MAX_LENGTH + BLOCK_SZIE_1 -1) / BLOCK_SZIE_1;
    if (lseek(fd, addrOffset * BLOCK_SZIE_1, SEEK_SET) < 0) {
        std::cout << "Failed to clean file\n";
        return;
    }
    if (write(fd, &buffer, sizeof(buffer)) != sizeof(buffer)) {
        std::cout << "clean misc failed\n";
        return;
    }
}

static void WriteLogoContent(int fd, const std::string &logoPath, uint32_t size)
{
    if (fd < 0 || logoPath.empty() || size <= 0) {
        std::cout << "path is null or size illegal\n";
        return;
    }
    FILE* rgbFile = fopen(logoPath.c_str(), "rb");
    if (rgbFile == nullptr) {
        std::cout << "cannot find pic file\n";
        return;
    }

    char* buffer = (char*)malloc(size);
    if (buffer == nullptr) {
        return;
    }
    uint32_t ret = fread(buffer, 1, size, rgbFile);
    if (ret < 0) {
        return;
    }
    ret = write(fd, buffer, size);
    if (ret != size) {
        return;
    }

    if (buffer != nullptr) {
        free(buffer);
        buffer = nullptr;
    }
    (void)fclose(rgbFile);
}

static int WriteLogo(int fd, const std::string &logoPath)
{
    if (fd < 0 || logoPath.empty()) {
        std::cout << "Invalid arguments\n";
        return -1;
    }
    int addrOffset = (PARTITION_INFO_POS + PARTITION_INFO_MAX_LENGTH + BLOCK_SZIE_1 - 1) / BLOCK_SZIE_1;
    if (lseek(fd, addrOffset * BLOCK_SZIE_1, SEEK_SET) < 0) {
        std::cout << "Failed to seek file\n";
        return -1;
    }

    uint32_t magic = 0;
    if (read(fd, &magic, sizeof(uint32_t)) != sizeof(uint32_t)) {
        std::cout << "Failed to read magic number\n";
        return -1;
    }

    if (magic == LOGO_MAGIC) {
        std::cout << "Get matched magic number, logo already written\n";
        return 0;
    }
    struct stat st {};
    magic = LOGO_MAGIC;
    lseek(fd, addrOffset * BLOCK_SZIE_1, SEEK_SET);
    if (write(fd, &magic, sizeof(magic)) != sizeof(magic)) {
        std::cout << "Write magic number failed\n";
        return -1;
    }

    if (stat(logoPath.c_str(), &st) < 0) {
        if (errno == ENOENT) {
            std::cout << logoPath << " is not exist\n";
        } else {
            std::cout << "Failed to get " << logoPath << " stat\n";
        }
        ClearLogo(fd);
        return -1;
    }

    if (st.st_size < 0 || st.st_size > updater::MAX_LOGO_SIZE) {
        std::cout << "Invalid logo file with size \n";
        ClearLogo(fd);
        return -1;
    }

    uint32_t logoSize =  static_cast<uint32_t>(st.st_size);
    if (write(fd, &logoSize, sizeof(logoSize)) != sizeof(logoSize)) {
        std::cout << "Write logo size failed\n";
        ClearLogo(fd);
        return -1;
    }

    WriteLogoContent(fd, logoPath, logoSize);
    return 0;
}

static void WriteLogoToMisc(const std::string &logoPath)
{
    if (logoPath.empty()) {
        std::cout << "logo path is empty\n";
        return;
    }
    std::string miscDev = GetMiscDevicePath();
    if (miscDev.empty()) {
        return;
    }

    int fd = open(miscDev.c_str(), O_RDWR | O_CLOEXEC, 0644);
    if (fd < 0) {
        std::cout << "Failed to open " << miscDev << ", err = " << errno << std::endl;
        return;
    }

    if (WriteLogo(fd, logoPath) < 0) {
        std::cout << "Write logo to " << miscDev << " failed" << std::endl;
    }
    close(fd);
    int addrOffset = (PARTITION_INFO_POS + PARTITION_INFO_MAX_LENGTH + BLOCK_SZIE_1 - 1) / BLOCK_SZIE_1;
    int fd1 = open(miscDev.c_str(), O_RDWR | O_CLOEXEC, 0644);
    if (lseek(fd1, addrOffset * BLOCK_SZIE_1, SEEK_SET) < 0) {
        std::cout << "Failed to seek file\n";
        return;
    }

    uint32_t magic = 0;
    uint32_t size = 0;
    if (read(fd1, &magic, sizeof(uint32_t)) != sizeof(uint32_t)) {
        std::cout << "Failed to read magic number\n";
        return;
    }
    if (read(fd1, &size, sizeof(uint32_t)) != sizeof(uint32_t)) {
        std::cout << "Failed to read magic number\n";
        return;
    }

    close(fd1);
}

int main(int argc, char **argv)
{
    int rc = -1;
    int optIndex = -1;
    while ((rc = getopt_long(argc, argv, "",  g_options, &optIndex)) != -1) {
        switch (rc) {
            case 0: {
                std::string optionName = g_options[optIndex].name;
                if (optionName == "write_logo") {
                    std::string logoPath = optarg;
                    WriteLogoToMisc(logoPath);
                }
                break;
            }
            case '?':
                std::cout << "Invalid arugment\n";
                break;
            default:
                std::cout << "Invalid arugment\n";
                break;
        }
    }
}