summaryrefslogtreecommitdiffstats
path: root/fs/fat/fat-pbl.c
blob: 93cd6decbcb03e25da9d59746231bc039507ffd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// SPDX-License-Identifier: GPL-2.0-only
/*
 * fat-pbl.c - PBL FAT filesystem barebox driver
 *
 * Copyright (c) 2019 Ahmad Fatoum, Pengutronix
 */

#define pr_fmt(fmt) "fat-pbl: " fmt

#include <common.h>
#include "integer.h"
#include "ff.h"
#include "diskio.h"
#include "pbl.h"

DRESULT disk_read(FATFS *fat, BYTE *buf, DWORD sector, BYTE count)
{
	int ret = pbl_bio_read(fat->userdata, sector, buf, count);
	return ret != count ? ret : 0;
}

ssize_t pbl_fat_load(struct pbl_bio *bio, const char *filename, void *dest, size_t len)
{
	FATFS	fs = {};
	FIL	file = {};
	UINT	nread;
	int	ret;

	fs.userdata = bio;

	/* mount fs */
	ret = f_mount(&fs);
	if (ret) {
		pr_debug("f_mount(%s) failed: %d\n", filename, ret);
		return ret;
	}

	ret = f_open(&fs, &file, filename, FA_OPEN_EXISTING | FA_READ);
	if (ret) {
		pr_debug("f_open(%s) failed: %d\n", filename, ret);
		return ret;
	}

	pr_debug("Reading file %s to 0x%p\n", filename, dest);

	ret = f_read(&file, dest, len, &nread);
	if (ret) {
		pr_debug("f_read failed: %d\n", ret);
		return ret;
	}

	return f_size(&file) <= len ? nread : -ENOSPC;
}