• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace OHOS {
25 namespace HDI {
26 namespace Partitionslot {
27 namespace V1_0 {
28 #define  HILOG_LOG_TAG           hdf_partitionslot_manager
29 
30 constexpr int32_t UPDATE_PARTITION_B = 2;
31 
32 constexpr off_t MISC_PARTITION_ACTIVE_SLOT_OFFSET = 1024;
33 constexpr off_t MISC_PARTITION_ACTIVE_SLOT_SIZE = 4;
34 
35 constexpr off_t MISC_PARTITION_UNBOOT_SLOT_OFFSET = MISC_PARTITION_ACTIVE_SLOT_OFFSET + MISC_PARTITION_ACTIVE_SLOT_SIZE;
36 constexpr off_t MISC_PARTITION_UNBOOT_SLOT_SIZE = 4;
37 
38 #define MISC_DEVICE_NODE "/dev/block/by-name/bootctrl"
39 
GetCurrentSlot(int32_t & currentSlot,int32_t & numOfSlots)40 int32_t PartitionSlotManager::GetCurrentSlot(int32_t& currentSlot, int32_t& numOfSlots)
41 {
42     HILOG_DEBUG(LOG_CORE, "%{public}s called!", __func__);
43     numOfSlots = system::GetIntParameter("ohos.boot.bootslots", 1);
44     currentSlot = ReadMisc(MISC_PARTITION_ACTIVE_SLOT_OFFSET, MISC_PARTITION_ACTIVE_SLOT_SIZE);
45     HILOG_INFO(LOG_CORE, "current slot is %{public}d, numOfSlots is %{public}d", currentSlot, numOfSlots);
46     return 0;
47 }
48 
GetSlotSuffix(int32_t slot,std::string & suffix)49 int32_t PartitionSlotManager::GetSlotSuffix(int32_t slot, std::string& suffix)
50 {
51     HILOG_DEBUG(LOG_CORE, "%{public}s called!", __func__);
52     if (slot == UPDATE_PARTITION_B) {
53         suffix = "_b";
54     } else {
55         suffix = "_a";
56     }
57     HILOG_INFO(LOG_CORE, "suffix is %{public}s", suffix.c_str());
58     return 0;
59 }
60 
SetActiveSlot(int32_t slot)61 int32_t PartitionSlotManager::SetActiveSlot(int32_t slot)
62 {
63     HILOG_DEBUG(LOG_CORE, "%{public}s called!, slot is %{public}d", __func__, slot);
64     return WriteSlotToMisc(slot, MISC_PARTITION_ACTIVE_SLOT_OFFSET, MISC_PARTITION_ACTIVE_SLOT_SIZE);
65 }
66 
SetSlotUnbootable(int32_t slot)67 int32_t PartitionSlotManager::SetSlotUnbootable(int32_t slot)
68 {
69     HILOG_DEBUG(LOG_CORE, "%{public}s called!, slot is %{public}d", __func__, slot);
70     return WriteSlotToMisc(slot, MISC_PARTITION_UNBOOT_SLOT_OFFSET, MISC_PARTITION_UNBOOT_SLOT_SIZE);
71 }
72 
WriteSlot(int fd,int32_t slot,off_t offset,off_t size)73 int32_t PartitionSlotManager::WriteSlot(int fd, int32_t slot, off_t offset, off_t size)
74 {
75     if (lseek(fd, offset, SEEK_SET) < 0) {
76         HILOG_ERROR(LOG_CORE, "Failed lseek slot %{public}d errno %{public}d", slot, errno);
77         return -1;
78     }
79 
80     if (write(fd, &slot, size) != size) {
81         HILOG_ERROR(LOG_CORE, "Write slot failed ");
82         return -1;
83     }
84     return 0;
85 }
86 
ReadMisc(off_t offset,off_t size)87 int PartitionSlotManager::ReadMisc(off_t offset, off_t size)
88 {
89     int fd = open(MISC_DEVICE_NODE, O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
90     if (fd < 0) {
91         return -1;
92     }
93     if (lseek(fd, offset, SEEK_SET) < 0) {
94         HILOG_ERROR(LOG_CORE, "Failed lseek miscDevice errno %{public}d ", errno);
95         close(fd);
96         return -1;
97     }
98     int32_t slot = 0;
99     if (read(fd, &slot, size) != size) {
100         HILOG_ERROR(LOG_CORE, "Failed read migic miscDevice errno %{public}d ", errno);
101         close(fd);
102         return -1;
103     }
104     HILOG_INFO(LOG_CORE, "Read slot %{public}d", slot);
105     close(fd);
106     return slot;
107 }
108 
WriteSlotToMisc(int32_t slot,off_t offset,off_t size)109 int PartitionSlotManager::WriteSlotToMisc(int32_t slot, off_t offset, off_t size)
110 {
111     if (slot < 0) {
112         HILOG_ERROR(LOG_CORE, "Invalid slot : %{public}d", slot);
113         return -1;
114     }
115     int fd = open(MISC_DEVICE_NODE, O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
116     if (fd < 0) {
117         HILOG_ERROR(LOG_CORE, "Failed to open miscDevice errno %{public}d ", errno);
118         return -1;
119     }
120 
121     if (WriteSlot(fd, slot, offset, size) < 0) {
122         HILOG_ERROR(LOG_CORE, "Failed to WriteSlot miscDevice %{public}d errno %{public}d ", slot, errno);
123         close(fd);
124         return -1;
125     }
126     close(fd);
127     return 0;
128 }
129 } // V1_0
130 } // Partitionslot
131 } // HDI
132 } // OHOS
133