summaryrefslogtreecommitdiffstats
path: root/lib/x1000-installer/test_lib/nand-x1000.h
blob: f34f2ce026273737ae215d1a9261b2a0f2abbd72 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2021 Aidan MacDonald
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

/* Stripped down fake version of X1000 NAND API for testing purposes,
 * uses a normal file to store the data */

#ifndef __NAND_X1000_H__
#define __NAND_X1000_H__

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

#define NAND_SUCCESS            0
#define NAND_ERR_UNKNOWN_CHIP   (-1)
#define NAND_ERR_PROGRAM_FAIL   (-2)
#define NAND_ERR_ERASE_FAIL     (-3)
#define NAND_ERR_UNALIGNED      (-4)
#define NAND_ERR_OTHER          (-5)
#define NAND_ERR_INJECTED       (-6)

/* keep max page size in sync with the NAND chip table in the .c file */
#define NAND_DRV_SCRATCHSIZE 32
#define NAND_DRV_MAXPAGESIZE 2112

typedef uint32_t nand_block_t;
typedef uint32_t nand_page_t;

enum nand_trace_type {
    NTT_READ,
    NTT_PROGRAM,
    NTT_ERASE,
};

enum nand_trace_exception {
    NTE_NONE,
    NTE_DOUBLE_PROGRAMMED,
    NTE_CLEARED,
};

struct nand_trace {
    enum nand_trace_type type;
    enum nand_trace_exception exception;
    nand_page_t addr;
};

typedef struct nand_chip {
    /* Base2 logarithm of the number of pages per block */
    unsigned log2_ppb;

    /* Size of a page's main / oob areas, in bytes. */
    unsigned page_size;
    unsigned oob_size;

    /* Total number of blocks in the chip */
    unsigned nr_blocks;
} nand_chip;

typedef struct nand_drv {
    /* Backing file */
    int fd;
    int metafd;
    int lock_count;

    unsigned refcount;
    uint8_t* scratch_buf;
    uint8_t* page_buf;
    const nand_chip* chip;
    unsigned ppb;
    unsigned fpage_size;
} nand_drv;

extern const char* nand_backing_file;
extern const char* nand_meta_file;

extern struct nand_trace* nand_trace;
extern size_t nand_trace_capacity;
extern size_t nand_trace_length;

extern void nand_trace_reset(size_t size);
extern void nand_inject_error(int rc);

extern nand_drv* nand_init(void);

extern void nand_lock(nand_drv* drv);
extern void nand_unlock(nand_drv* drv);

extern int nand_open(nand_drv* drv);
extern void nand_close(nand_drv* drv);
extern int nand_block_erase(nand_drv* drv, nand_block_t block);
extern int nand_page_program(nand_drv* drv, nand_page_t page, const void* buffer);
extern int nand_page_read(nand_drv* drv, nand_page_t page, void* buffer);

#endif /* __NAND_X1000_H__ */