Files
oxmc cf89fbad2a recovery_toolkit/twrpinstall: add block_device_utils.cpp -- get_block_device_size/wipe_block_device copied from ext4_utils
Same reasoning as the SnapshotManager case: a declared static_libs edge
to libext4_utils didn't resolve these two symbols in practice, and
libext4_utils can't be whole_static_libs'd without colliding with
libsnapshot's internal copy. Both functions are tiny and self-contained
-- copied directly, extern "C" matching the real ext4_utils.h/wipe.h
declarations exactly (install.cpp's actual header include).
2026-09-30 17:59:28 -07:00

97 lines
2.7 KiB
C++

/*
* Copyright (C) 2011 The Android Open Source Project
*
* 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.
*/
// get_block_device_size/wipe_block_device/is_block_device_fd, extracted
// from system/extras/ext4_utils/ext4_utils.cpp and wipe.cpp -- copied
// directly instead of merging libext4_utils as a whole dependency, since
// libext4_utils's own static variant is already whole_static_libs'd
// internally by libsnapshot (duplicate-symbol risk, same class of problem
// documented in recovery_ui/Android.bp) and a plain declared static_libs
// edge did not resolve these two symbols in practice. install.cpp
// (twrpinstall) is the only real caller, via the real
// ext4_utils/ext4_utils.h + ext4_utils/wipe.h headers (already an
// include_dir here) -- extern "C" below must match those headers' real
// declarations exactly or this hits the same linkage-mismatch bug gui.h
// did earlier. See notes/recovery-work.md.
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/fs.h>
#include <sys/ioctl.h>
#ifndef BLKDISCARD
#define BLKDISCARD _IO(0x12, 119)
#endif
#ifndef BLKSECDISCARD
#define BLKSECDISCARD _IO(0x12, 125)
#endif
typedef uint64_t u64;
typedef int64_t s64;
extern "C" {
int is_block_device_fd(int fd) {
struct stat st;
int ret = fstat(fd, &st);
if (ret < 0) return 0;
return S_ISBLK(st.st_mode);
}
u64 get_block_device_size(int fd) {
u64 size = 0;
int ret = ioctl(fd, BLKGETSIZE64, &size);
if (ret) return 0;
return size;
}
int wipe_block_device(int fd, s64 len) {
uint64_t range[2];
int ret;
if (!is_block_device_fd(fd)) {
// Wiping only makes sense on a block device.
return 0;
}
range[0] = 0;
range[1] = len;
ret = ioctl(fd, BLKSECDISCARD, &range);
if (ret < 0) {
range[0] = 0;
range[1] = len;
ret = ioctl(fd, BLKDISCARD, &range);
if (ret < 0) {
return 1;
} else {
char buf[4096] = {0};
if (write(fd, buf, sizeof(buf)) != (ssize_t)sizeof(buf)) {
return 1;
}
fsync(fd);
return 0;
}
}
return 0;
}
} // extern "C"