Writing to absolute sectors on SD Card

Robert S. Grimes rsg at alum.mit.edu
Thu Oct 23 16:27:10 UTC 2008

Is it possible to write to absolute sectors using the spi-sd-card driver?

Here is my approach, in rough terms:

  // Open the SD card device
  int raw = open("/dev/sdcarda", O_WRONLY);
  printf("Open device returned %d\n", raw);
  if (raw > 0) {
    // Get some data into buffer
    uint8_t buffer[512];
    for (int i = 0; i < 512; ++i) {
      buffer[i] = (uint8_t)i;

    // Write it to the first sector
    unsigned block = 0;
    int numWritten = 1;
    numWritten = write(raw, buffer, 512);
    if (numWritten != 512) {
      printf("NumWritten = %d\n", numWritten);

    // Close the file
I would expect this to either work, or perror would print some error; 
however, neither event occurs; instead, this is what I get:

    Open device returned 4
    NumWritten = 0

So nothing was written ("NumWritten = 0"), and no error occurred (no 
error after "Aborting:")!  Any ideas what I've done wrong here?


More information about the users mailing list