summaryrefslogtreecommitdiff
path: root/cgpt/flash_ts_drv.c
blob: 8c918866285ebfcaf3472c367994cb8c80456ca1 (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
/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Utility for ChromeOS-specific GPT partitions, Please see corresponding .c
 * files for more details.
 */

#include "flash_ts.h"

#include "cgpt.h"
#include "errno.h"
#include "stdio.h"
#include "string.h"

inline int page_to_sector(const nand_geom *nand, int page) {
  return page * (nand->szofpg / nand->szofsector);
}

int nand_read_page(const nand_geom *nand, int page, void *buf, int size) {
  uint8_t *page_buff;

  if (size > nand->szofpg) {
    return -1;
  }
  if (Load((struct drive *)nand->user, &page_buff,
           page_to_sector(nand, page), nand->szofsector,
           (size + nand->szofsector - 1) / nand->szofsector)) {

    // page may be not erased. return default data.
    memset(buf, 0xff, size);
    return 0;
  }
  memcpy(buf, page_buff, size);
  free(page_buff);
  return 0;
}

int nand_write_page(const nand_geom *nand,
                    int page, const void *buf, int size) {
  void *page_buff;
  int ret;

  if (size > nand->szofpg) {
    return -1;
  }
  page_buff  = malloc(nand->szofpg);
  if (!page_buff)
    return -1;

  memset(page_buff, 0xff, nand->szofpg);
  memcpy(page_buff, buf, size < nand->szofpg ? size : nand->szofpg);

  ret = Save((struct drive *)nand->user, page_buff, page_to_sector(nand, page),
             nand->szofsector, nand->szofpg / nand->szofsector);
  free(page_buff);
  return ret;
}

int nand_erase_block(const nand_geom *nand, int block) {
  int sector = block * (nand->szofblk / nand->szofsector);
  int res;
  void *erase_buff = malloc(nand->szofblk);
  if (!erase_buff)
    return -1;

  memset(erase_buff, 0xff, nand->szofblk);
  res = Save((struct drive *)nand->user, erase_buff, sector,
             nand->szofsector, nand->szofblk / nand->szofsector);
  free(erase_buff);
  return res;
}

int nand_is_bad_block(const nand_geom *nand, int block) {
  return 0;
}