3

I am trying to write a cross-platform (Linux, Mac OS and windows) tool/script which can write .img images to SD cards via an SD card reader connected to the computer. I tried searching a lot for tutorials/references on how this could be done using various languages but I was unable to find anything fruitful.

I want to gain a deeper understanding of the underlying process which happens when images are written to SD cards, and what factors make this process platform dependent. Some kind of guide/blog post of how such a program can be implemented in some language would be wonderful. (dd command can be used on linux and mac os, but I'm exploring the possibility of writing a single uniform program which could do the job on all platforms)

I would like some guidance/references regarding this

fuz
  • 88,405
  • 25
  • 200
  • 352
Quark
  • 218
  • 2
  • 14
  • No chance to do this cross-platform, at least not on Windows. OS X and Linux should have roughly similar ways to do this. What is a `.img` file? That suffix is used for a ton of purposes. – fuz Mar 12 '15 at 20:33
  • 1
    .img file containing a bootable OS – Quark Mar 13 '15 at 10:00

1 Answers1

2

From the perspective of an application program, an SD card is just a file. You can write data on the SD card with the same library functions and system calls you would normally use. On Unix-like operating systems, the files corresponding to devices are commonly placed in the folder /dev. For instance, to write the image sd.img on the first SD card on Linux, you could invoke the command dd like this:

dd if=sd.img of=/dev/mmcblk0

This copies the content of sd.img into the SD card. The process is similar but not exactly equal on other platforms.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • If they are treated as normal files, then why can't we simply use filesystem access functions (like fopen, fwrite in C). Why is there a necessity for a specialized program like dd – Quark Mar 13 '15 at 13:48
  • 1
    You could also use `cat` as in `cat sd.img >/dev/mmcblk0` or any other programs but `dd` has options `cat` doesn't have that come in handy when you want to copy image files. You can of course also use file system functions like `fopen` or `fwrite`. – fuz Mar 13 '15 at 13:51
  • Alright, I kind of understand now. Thanks a lot for the help @FUZxxl – Quark Mar 13 '15 at 16:49