1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "partitionslot_manager.h"
17 
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <linux/limits.h>
21 #include "param_wrapper.h"
22 #include "hilog/log.h"
23 
24 #undef LOG_DOMAIN
25 #undef LOG_TAG
26 #define LOG_DOMAIN 0xD002500
27 #define LOG_TAG "hdf_partitionslot_manager"
28 
29 namespace OHOS {
30 namespace HDI {
31 namespace Partitionslot {
32 namespace V1_0 {
33 
34 constexpr int32_t UPDATE_PARTITION_B = 2;
35 
36 constexpr off_t MISC_PARTITION_ACTIVE_SLOT_OFFSET = 1024;
37 constexpr off_t MISC_PARTITION_ACTIVE_SLOT_SIZE = 4;
38 
39 constexpr off_t MISC_PARTITION_UNBOOT_SLOT_OFFSET = MISC_PARTITION_ACTIVE_SLOT_OFFSET + MISC_PARTITION_ACTIVE_SLOT_SIZE;
40 constexpr off_t MISC_PARTITION_UNBOOT_SLOT_SIZE = 4;
41 
42 #define MISC_DEVICE_NODE "/dev/block/by-name/bootctrl"
43 
GetCurrentSlot(int32_t & currentSlot,int32_t & numOfSlots)44 int32_t PartitionSlotManager::GetCurrentSlot(int32_t& currentSlot, int32_t& numOfSlots)
45 {
46     HILOG_DEBUG(LOG_CORE, "%{public}s called!", __func__);
47     numOfSlots = system::GetIntParameter("ohos.boot.bootslots", 1);
48     currentSlot = ReadMisc(MISC_PARTITION_ACTIVE_SLOT_OFFSET, MISC_PARTITION_ACTIVE_SLOT_SIZE);
49     HILOG_INFO(LOG_CORE, "current slot is %{public}d, numOfSlots is %{public}d", currentSlot, numOfSlots);
50     return 0;
51 }
52 
GetSlotSuffix(int32_t slot,std::string & suffix)53 int32_t PartitionSlotManager::GetSlotSuffix(int32_t slot, std::string& suffix)
54 {
55     HILOG_DEBUG(LOG_CORE, "%{public}s called!", __func__);
56     if (slot == UPDATE_PARTITION_B) {
57         suffix = "_b";
58     } else {
59         suffix = "_a";
60     }
61     HILOG_INFO(LOG_CORE, "suffix is %{public}s", suffix.c_str());
62     return 0;
63 }
64 
SetActiveSlot(int32_t slot)65 int32_t PartitionSlotManager::SetActiveSlot(int32_t slot)
66 {
67     HILOG_DEBUG(LOG_CORE, "%{public}s called!, slot is %{public}d", __func__, slot);
68     return WriteSlotToMisc(slot, MISC_PARTITION_ACTIVE_SLOT_OFFSET, MISC_PARTITION_ACTIVE_SLOT_SIZE);
69 }
70 
SetSlotUnbootable(int32_t slot)71 int32_t PartitionSlotManager::SetSlotUnbootable(int32_t slot)
72 {
73     HILOG_DEBUG(LOG_CORE, "%{public}s called!, slot is %{public}d", __func__, slot);
74     return WriteSlotToMisc(slot, MISC_PARTITION_UNBOOT_SLOT_OFFSET, MISC_PARTITION_UNBOOT_SLOT_SIZE);
75 }
76 
WriteSlot(int fd,int32_t slot,off_t offset,off_t size)77 int32_t PartitionSlotManager::WriteSlot(int fd, int32_t slot, off_t offset, off_t size)
78 {
79     if (lseek(fd, offset, SEEK_SET) < 0) {
80         HILOG_ERROR(LOG_CORE, "Failed lseek slot %{public}d errno %{public}d", slot, errno);
81         return -1;
82     }
83 
84     if (write(fd, &slot, size) != size) {
85         HILOG_ERROR(LOG_CORE, "Write slot failed ");
86         return -1;
87     }
88     return 0;
89 }
90 
ReadMisc(off_t offset,off_t size)91 int PartitionSlotManager::ReadMisc(off_t offset, off_t size)
92 {
93     int fd = open(MISC_DEVICE_NODE, O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
94     if (fd < 0) {
95         return -1;
96     }
97     if (lseek(fd, offset, SEEK_SET) < 0) {
98         HILOG_ERROR(LOG_CORE, "Failed lseek miscDevice errno %{public}d ", errno);
99         close(fd);
100         return -1;
101     }
102     int32_t slot = 0;
103     if (read(fd, &slot, size) != size) {
104         HILOG_ERROR(LOG_CORE, "Failed read migic miscDevice errno %{public}d ", errno);
105         close(fd);
106         return -1;
107     }
108     HILOG_INFO(LOG_CORE, "Read slot %{public}d", slot);
109     close(fd);
110     return slot;
111 }
112 
WriteSlotToMisc(int32_t slot,off_t offset,off_t size)113 int PartitionSlotManager::WriteSlotToMisc(int32_t slot, off_t offset, off_t size)
114 {
115     if (slot < 0) {
116         HILOG_ERROR(LOG_CORE, "Invalid slot : %{public}d", slot);
117         return -1;
118     }
119     int fd = open(MISC_DEVICE_NODE, O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
120     if (fd < 0) {
121         HILOG_ERROR(LOG_CORE, "Failed to open miscDevice errno %{public}d ", errno);
122         return -1;
123     }
124 
125     if (WriteSlot(fd, slot, offset, size) < 0) {
126         HILOG_ERROR(LOG_CORE, "Failed to WriteSlot miscDevice %{public}d errno %{public}d ", slot, errno);
127         close(fd);
128         return -1;
129     }
130     close(fd);
131     return 0;
132 }
133 } // V1_0
134 } // Partitionslot
135 } // HDI
136 } // OHOS
137