summaryrefslogtreecommitdiff
path: root/cgpt/drive.c
blob: db6601d1a83749b17a2ca9ebf335f1bbbbced2cb (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* Copyright 2014 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.
 */

#include <errno.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>

#include "cgpt.h"
#include "fmap.h"

// TODO(namnguyen): Remove RW_UNUSED
#ifdef DEBUG
static const char FMAP_GPT_SECTION[] = "RW_UNUSED";
#else
static const char FMAP_GPT_SECTION[] = "RW_GPT";
#endif

off_t FileSeek(struct drive* drive, off_t offset, int whence) {
  return lseek(drive->fd, offset, whence);
}

ssize_t FileRead(struct drive* drive, void* buf, size_t count) {
  return read(drive->fd, buf, count);
}

ssize_t FileWrite(struct drive* drive, const void* buf, size_t count) {
  return write(drive->fd, buf, count);
}

int FileSync(struct drive* drive) {
  return fsync(drive->fd);
}

int FileClose(struct drive* drive) {
  return close(drive->fd);
}

// Always terminate the buffer after snprintf.
static int tsnprintf(char *buf, size_t size, const char* fmt, ...) {
  if (size == 0) {
    // No space for the null char.
    errno = ENOSPC;
    return -1;
  }
  va_list ap;
  va_start(ap, fmt);
  int ret = vsnprintf(buf, size, fmt, ap);
  va_end(ap);
  if (ret >= 0) {
    buf[size - 1] = '\x00';
  }
  return ret;
}

int FlashInit(struct drive* drive) {
  int return_code = 1;
  char tempdir[] = "/tmp/cgptXXXXXX";
  if (mkdtemp(tempdir) == NULL) {
    Error("Cannot create temp directory for flashrom work.\n");
    return return_code;
  }

  char cmd[256];
  char fmap_name[28];
  tsnprintf(fmap_name, sizeof(fmap_name), "%s/fmap", tempdir);
  tsnprintf(cmd, sizeof(cmd), "/usr/sbin/flashrom -p host -i FMAP:%s -r "
            "> /dev/null 2>&1", fmap_name);
  return_code++;
  if (system(cmd) != 0) {
    Error("Cannot dump FMAP section from flash.\n");
    goto cleanup;
  };

  return_code++;
  int fmap_fd = open(fmap_name, O_RDONLY);
  if (fmap_fd < 0) {
    Error("Cannot open %s.\n", fmap_name);
    goto cleanup;
  }
  // Allocate 4096 bytes. ChromeOS FMAP is usually 2048 bytes.
  return_code++;
  const size_t fmap_alloc_size = 4096;
  uint8_t* fmap = malloc(fmap_alloc_size);
  if (!fmap) {
    Error("Cannot read fmap.\n");
    goto cleanup2;
  }
  return_code++;
  int fmap_size = read(fmap_fd, fmap, fmap_alloc_size);
  if (fmap_size < 0) {
    Error("Cannot read from %s.\n", fmap_name);
    goto cleanup3;
  }

  return_code++;
  FmapAreaHeader* gpt_area;
  if (fmap_find_by_name(fmap, fmap_size, NULL,
                        FMAP_GPT_SECTION, &gpt_area) == NULL) {
    Error("Cannot find GPT section in the FMAP.\n");
    goto cleanup3;
  }

  drive->flash_start = gpt_area->area_offset;
  drive->flash_size = gpt_area->area_size;
  drive->current_position = 0;

  return_code = 0;

cleanup3:
  free(fmap);
cleanup2:
  close(fmap_fd);
cleanup:
  tsnprintf(cmd, sizeof(cmd), "/bin/rm -rf %s", tempdir);
  if (system(cmd)) {
    Warning("Cannot remove temp directory", tempdir);
  }
  return return_code;
}

off_t FlashSeek(struct drive* drive, off_t offset, int whence) {
  off_t new_position;
  switch (whence) {
    case SEEK_SET:
      new_position = offset;
      break;
    case SEEK_CUR:
      new_position = drive->current_position + offset;
      break;
    case SEEK_END:
      new_position = drive->size + offset;
      break;
    default:
      errno = EINVAL;
      return -1;
  }
  if (new_position < 0 || new_position > drive->size) {
    errno = EINVAL;
    return -1;
  }
  drive->current_position = new_position;
  return new_position;
}

// Translate |position| to an address in flash.
// We only use a small area in flash to store the GPT structures. This area is
// identified in FMAP. So the idea is to map |position| from 0 to flash_size to
// the physical position in flash linearly.
// This function returns 0 for success.
static int TranslateToFlash(struct drive* drive, off_t position, size_t count,
                            off_t* translated) {
  if (position < 0 || position + count > drive->flash_size) {
    return -1;
  }
  *translated = position + drive->flash_start;
  return 0;
}

static int CreateLayout(char* file_name, off_t position, size_t count) {
  int fd = mkstemp(file_name);
  if (fd < 0) {
    Error("Cannot create layout file.\n");
    return -1;
  }
  char buf[128];
  tsnprintf(buf, sizeof(buf), "%08X:%08X landmark\n", (unsigned int) position,
            (unsigned int) (position + count - 1));
  int layout_len = strlen(buf);
  int nr_written = write(fd, buf, layout_len);
  close(fd);
  if (nr_written != layout_len) {
    Error("Cannot write out layout for flashrom.\n");
    return -1;
  }

  return 0;
}

ssize_t FlashRead(struct drive* drive, void* buf, size_t count) {
  off_t offset;
  if (TranslateToFlash(drive, drive->current_position, count, &offset) != 0) {
    Error("Cannot translate disk address %08X to SPI address.\n",
          drive->current_position);
    errno = EINVAL;
    return -1;
  }

  char tempdir[] = "/tmp/cgptXXXXXX";
  if (mkdtemp(tempdir) == NULL) {
    Error("Cannot create temp directory for flashrom work.\n");
    errno = EIO;
    return -1;
  }

  int return_value = -1;
  char layout_file[40];
  tsnprintf(layout_file, sizeof(layout_file), "%s/layoutXXXXXX", tempdir);
  if (CreateLayout(layout_file, offset, count) != 0) {
    Error("Cannot create layout file for flashrom.\n");
    goto cleanup;
  }

  char content_file[40];
  tsnprintf(content_file, sizeof(content_file), "%s/contentXXXXXX", tempdir);
  int fd = mkstemp(content_file);
  if (fd < 0) {
    goto cleanup;
  }

  char cmd[256];
  tsnprintf(cmd, sizeof(cmd), "/usr/sbin/flashrom -p host -l %s -i landmark:%s "
            "-r > /dev/null 2>&1", layout_file, content_file);
  if (system(cmd) != 0) {
    Error("Cannot read from SPI flash.\n");
    goto cleanup2;
  }

  return_value = read(fd, buf, count);
  if (return_value != count) {
    Error("Cannot read from retrieved content file.\n");
    return_value = -1;
  } else {
    drive->current_position += return_value;
  }

cleanup2:
  close(fd);
cleanup:
  tsnprintf(cmd, sizeof(cmd), "/bin/rm -rf %s", tempdir);
  if (system(cmd)) {
    Warning("Cannot remove temp directory", tempdir);
  }
  errno = EIO;
  return return_value;
}

ssize_t FlashWrite(struct drive* drive, const void* buf, size_t count) {
  off_t offset;
  if (TranslateToFlash(drive, drive->current_position, count, &offset) != 0) {
    Error("Cannot translate disk address %08X to SPI address.\n",
          drive->current_position);
    errno = EINVAL;
    return -1;
  }

  char tempdir[] = "/tmp/cgptXXXXXX";
  if (mkdtemp(tempdir) == NULL) {
    Error("Cannot create temp directory for flashrom work.\n");
    errno = EIO;
    return -1;
  }

  int return_value = -1;
  char layout_file[40];
  tsnprintf(layout_file, sizeof(layout_file), "%s/layoutXXXXXX", tempdir);
  if (CreateLayout(layout_file, offset, count) != 0) {
    Error("Cannot create layout file for flashrom.\n");
    goto cleanup;
  }

  char content_file[40];
  tsnprintf(content_file, sizeof(content_file), "%s/contentXXXXXX", tempdir);
  int fd = mkstemp(content_file);
  if (fd < 0) {
    goto cleanup;
  }

  return_value = write(fd, buf, count);
  close(fd);
  if (return_value != count) {
    Error("Cannot prepare content file for flashrom.\n");
    return_value = -1;
    goto cleanup;
  }

  char cmd[256];
  // TODO(namnguyen): Add --fast-verify after 428475 is fixed
  tsnprintf(cmd, sizeof(cmd), "/usr/sbin/flashrom -p host -l %s -i landmark:%s "
            "-w > /dev/null 2>&1", layout_file, content_file);
  if (system(cmd) != 0) {
    Error("Cannot write to SPI flash.\n");
    return_value = -1;
  } else {
    drive->current_position += return_value;
  }

cleanup:
  tsnprintf(cmd, sizeof(cmd), "/bin/rm -rf %s", tempdir);
  if (system(cmd)) {
    Warning("Cannot remove temp directory", tempdir);
  }
  errno = EIO;
  return return_value;
}

int FlashSync(struct drive* drive) {
  return 0;
}

int FlashClose(struct drive* drive) {
  return 0;
}