1 /*
2 * Copyright (c) 2023 Antmicro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/fs/fs.h>
9 #include <zephyr/storage/disk_access.h>
10
11 #include "utils.h"
12
sectors_info(const char * disk,uint32_t * ss,uint32_t * sc)13 static int sectors_info(const char *disk, uint32_t *ss, uint32_t *sc)
14 {
15 int rc;
16
17 rc = disk_access_ioctl(disk, DISK_IOCTL_GET_SECTOR_COUNT, sc);
18 if (rc < 0) {
19 TC_PRINT("Disk access (sector count) error: %d", rc);
20 return rc;
21 }
22
23 rc = disk_access_ioctl(disk, DISK_IOCTL_GET_SECTOR_SIZE, ss);
24 if (rc < 0) {
25 TC_PRINT("Disk access (sector size) error: %d", rc);
26 return rc;
27 }
28
29 return 0;
30 }
31
wipe_partition(uintptr_t id)32 int wipe_partition(uintptr_t id)
33 {
34 int rc;
35 const char *name = (const char *)id;
36 uint32_t sector_count, sector_size;
37
38 TC_PRINT("Wiping %s\n", name);
39
40 rc = disk_access_init(name);
41 if (rc < 0) {
42 return rc;
43 }
44
45 rc = sectors_info(name, §or_size, §or_count);
46 if (rc < 0) {
47 return rc;
48 }
49
50 uint8_t zeros[sector_size];
51
52 memset(zeros, 0, sector_size);
53
54 /* Superblock is located at offset 1024B and has size 1024B */
55 uint32_t start_sector = CONFIG_EXT2_DISK_STARTING_SECTOR + 1024 / sector_size;
56 uint32_t num_sectors = 1024 / sector_size + (1024 % sector_size != 0);
57
58 for (uint32_t i = 0; i < num_sectors; i++) {
59 rc = disk_access_write(name, zeros, start_sector + i, 1);
60 if (rc < 0) {
61 return rc;
62 }
63 }
64
65 return 0;
66 }
67
get_partition_size(uintptr_t id)68 size_t get_partition_size(uintptr_t id)
69 {
70 const char *name = (const char *)id;
71 uint32_t sector_count, sector_size;
72
73 sectors_info(name, §or_size, §or_count);
74
75 return sector_size * sector_count;
76 }
77