mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-06-06 17:41:24 -04:00
emunand: Add multipart support for rawnand images in fusee
This commit is contained in:
parent
a1512cf30f
commit
017d473b99
9 changed files with 438 additions and 54 deletions
|
@ -14,7 +14,7 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "device_partition.h"
|
||||
|
||||
|
@ -27,7 +27,7 @@ int device_partition_read_data(device_partition_t *devpart, void *dst, uint64_t
|
|||
return rc;
|
||||
}
|
||||
}
|
||||
if (devpart->read_cipher != NULL && devpart->crypto_mode != DevicePartitionCryptoMode_None) {
|
||||
if ((devpart->read_cipher != NULL) && (devpart->crypto_mode != DevicePartitionCryptoMode_None)) {
|
||||
for (uint64_t i = 0; i < num_sectors; i += devpart->crypto_work_buffer_num_sectors) {
|
||||
uint64_t n = (i + devpart->crypto_work_buffer_num_sectors > num_sectors) ? (num_sectors - i) : devpart->crypto_work_buffer_num_sectors;
|
||||
rc = devpart->reader(devpart, devpart->crypto_work_buffer, sector + i, n);
|
||||
|
@ -55,7 +55,7 @@ int device_partition_write_data(device_partition_t *devpart, const void *src, ui
|
|||
return rc;
|
||||
}
|
||||
}
|
||||
if (devpart->read_cipher != NULL && devpart->crypto_mode != DevicePartitionCryptoMode_None) {
|
||||
if ((devpart->write_cipher != NULL) && (devpart->crypto_mode != DevicePartitionCryptoMode_None)) {
|
||||
for (uint64_t i = 0; i < num_sectors; i += devpart->crypto_work_buffer_num_sectors) {
|
||||
uint64_t n = (i + devpart->crypto_work_buffer_num_sectors > num_sectors) ? (num_sectors - i) : devpart->crypto_work_buffer_num_sectors;
|
||||
memcpy(devpart->crypto_work_buffer, src + (size_t)(devpart->sector_size * i), (size_t)(devpart->sector_size * n));
|
||||
|
@ -74,28 +74,146 @@ int device_partition_write_data(device_partition_t *devpart, const void *src, ui
|
|||
}
|
||||
}
|
||||
|
||||
int emu_device_partition_read_data(device_partition_t *devpart, void *dst, uint64_t sector, uint64_t num_sectors, const char *origin_path)
|
||||
int emu_device_partition_read_data(device_partition_t *devpart, void *dst, uint64_t sector, uint64_t num_sectors, const char *origin_path, int num_parts, uint64_t part_limit)
|
||||
{
|
||||
int rc = 0;
|
||||
uint64_t target_sector = 0;
|
||||
char target_path[0x300 + 1] = {0};
|
||||
|
||||
/* Read partition data using our backing file. */
|
||||
FILE *origin = fopen(origin_path, "rb");
|
||||
fseek(origin, sector * devpart->sector_size, SEEK_CUR);
|
||||
rc = (fread(dst, devpart->sector_size, num_sectors, origin) > 0) ? 0 : -1;
|
||||
fclose(origin);
|
||||
/* Perform initialization steps, if necessary. */
|
||||
if (!devpart->initialized) {
|
||||
rc = devpart->initializer(devpart);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
/* Handle data in multiple parts, if necessary. */
|
||||
if (num_parts > 0) {
|
||||
int target_part = 0;
|
||||
uint64_t data_offset = sector * devpart->sector_size;
|
||||
|
||||
if (data_offset >= part_limit) {
|
||||
uint64_t data_offset_aligned = (data_offset + (part_limit - 1)) & ~(part_limit - 1);
|
||||
target_part = (data_offset_aligned == data_offset) ? (data_offset / part_limit) : (data_offset_aligned / part_limit) - 1;
|
||||
target_sector = (data_offset - (target_part * part_limit)) / devpart->sector_size;
|
||||
|
||||
/* Target part is invalid. */
|
||||
if (target_part > num_parts) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
snprintf(target_path, sizeof(target_path) - 1, "%s%02d", origin_path, target_part);
|
||||
} else {
|
||||
target_sector = sector;
|
||||
strcpy(target_path, origin_path);
|
||||
}
|
||||
|
||||
/* Read the partition data. */
|
||||
if ((devpart->read_cipher != NULL) && (devpart->crypto_mode != DevicePartitionCryptoMode_None)) {
|
||||
for (uint64_t i = 0; i < num_sectors; i += devpart->crypto_work_buffer_num_sectors) {
|
||||
uint64_t n = (i + devpart->crypto_work_buffer_num_sectors > num_sectors) ? (num_sectors - i) : devpart->crypto_work_buffer_num_sectors;
|
||||
|
||||
/* Read partition data using our backing file. */
|
||||
FILE *origin = fopen(target_path, "rb");
|
||||
fseek(origin, (target_sector + i) * devpart->sector_size, SEEK_CUR);
|
||||
rc = (fread(dst, devpart->sector_size, n, origin) > 0) ? 0 : -1;
|
||||
fclose(origin);
|
||||
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Decrypt partition data. */
|
||||
rc = devpart->read_cipher(devpart, target_sector + i, n);
|
||||
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Copy partition data to destination. */
|
||||
memcpy(dst + (size_t)(devpart->sector_size * i), devpart->crypto_work_buffer, (size_t)(devpart->sector_size * n));
|
||||
}
|
||||
} else {
|
||||
/* Read partition data using our backing file. */
|
||||
FILE *origin = fopen(target_path, "rb");
|
||||
fseek(origin, target_sector * devpart->sector_size, SEEK_CUR);
|
||||
rc = (fread(dst, devpart->sector_size, num_sectors, origin) > 0) ? 0 : -1;
|
||||
fclose(origin);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int emu_device_partition_write_data(device_partition_t *devpart, const void *src, uint64_t sector, uint64_t num_sectors, const char *origin_path)
|
||||
int emu_device_partition_write_data(device_partition_t *devpart, const void *src, uint64_t sector, uint64_t num_sectors, const char *origin_path, int num_parts, uint64_t part_limit)
|
||||
{
|
||||
int rc = 0;
|
||||
uint64_t target_sector = 0;
|
||||
char target_path[0x300 + 1] = {0};
|
||||
|
||||
/* Write partition data using our backing file. */
|
||||
FILE *origin = fopen(origin_path, "wb");
|
||||
fseek(origin, sector * devpart->sector_size, SEEK_CUR);
|
||||
rc = (fwrite(src, devpart->sector_size, num_sectors, origin) > 0) ? 0 : -1;
|
||||
fclose(origin);
|
||||
/* Perform initialization steps, if necessary. */
|
||||
if (!devpart->initialized) {
|
||||
rc = devpart->initializer(devpart);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
/* Handle data in multiple parts, if necessary. */
|
||||
if (num_parts > 0) {
|
||||
int target_part = 0;
|
||||
uint64_t data_offset = sector * devpart->sector_size;
|
||||
|
||||
if (data_offset >= part_limit) {
|
||||
uint64_t data_offset_aligned = (data_offset + (part_limit - 1)) & ~(part_limit - 1);
|
||||
target_part = (data_offset_aligned == data_offset) ? (data_offset / part_limit) : (data_offset_aligned / part_limit) - 1;
|
||||
target_sector = (data_offset - (target_part * part_limit)) / devpart->sector_size;
|
||||
|
||||
/* Target part is invalid. */
|
||||
if (target_part > num_parts) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
snprintf(target_path, sizeof(target_path) - 1, "%s%02d", origin_path, target_part);
|
||||
} else {
|
||||
target_sector = sector;
|
||||
strcpy(target_path, origin_path);
|
||||
}
|
||||
|
||||
/* Write the partition data. */
|
||||
if ((devpart->write_cipher != NULL) && (devpart->crypto_mode != DevicePartitionCryptoMode_None)) {
|
||||
for (uint64_t i = 0; i < num_sectors; i += devpart->crypto_work_buffer_num_sectors) {
|
||||
uint64_t n = (i + devpart->crypto_work_buffer_num_sectors > num_sectors) ? (num_sectors - i) : devpart->crypto_work_buffer_num_sectors;
|
||||
|
||||
/* Copy partition data from source. */
|
||||
memcpy(devpart->crypto_work_buffer, src + (size_t)(devpart->sector_size * i), (size_t)(devpart->sector_size * n));
|
||||
|
||||
/* Encrypt data. */
|
||||
rc = devpart->write_cipher(devpart, target_sector + i, n);
|
||||
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Write partition data using our backing file. */
|
||||
FILE *origin = fopen(target_path, "wb");
|
||||
fseek(origin, (target_sector + i) * devpart->sector_size, SEEK_CUR);
|
||||
rc = (fwrite(src, devpart->sector_size, n, origin) > 0) ? 0 : -1;
|
||||
fclose(origin);
|
||||
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* Write partition data using our backing file. */
|
||||
FILE *origin = fopen(target_path, "wb");
|
||||
fseek(origin, sector * devpart->sector_size, SEEK_CUR);
|
||||
rc = (fwrite(src, devpart->sector_size, num_sectors, origin) > 0) ? 0 : -1;
|
||||
fclose(origin);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue