デジタルちんぶろぐ

デジタルな話題

ホーム 連絡をする 同期する ( RSS 2.0 ) Login
投稿数  268  : 記事  0  : コメント  4375  : トラックバック  79

ニュース


技術以外は
ちんぶろぐ

記事カテゴリ

書庫

日記カテゴリ

Windowsマシンでストレージからセクタ単位で読み出し、書き込みを行うためにはCreateFileで\\.\PHYSICALDRIVEx (xはドライブの番号、0からスタート)をファイル名(?)にしてオープンすればいいらしい。

 

で、お試し。折角なのでセクタ0(MBR)を読んでパーティションテーブルを見てみた。

環境はg++ 3.4.4 on Cygwin/ EeePC

----------プログラム----------

#include <windows.h>
#include <iostream>

using namespace std;

struct p_table {
    BYTE boot_flag;     // ブートフラグ
    BYTE start_chs[3];  // 開始CHS
    BYTE type;  // パーティションの種類
    BYTE end_chs[3];  // 終了CHS
    DWORD start_lba;  // 開始LBA
    DWORD total_sector; // セクタ数
};

int main(void)
{
    HANDLE h = CreateFile("\\\\.\\PHYSICALDRIVE0",
                          GENERIC_READ,
                          FILE_SHARE_READ | FILE_SHARE_WRITE,
                          NULL,
                          OPEN_EXISTING,
                          FILE_ATTRIBUTE_NORMAL,
                          NULL);
    if(h == INVALID_HANDLE_VALUE) {
        cout << "Could not open physical drive 0" << endl;
        return 1;
    }

    BYTE buf[0x200];
    DWORD read_size;

    ReadFile(h, buf, 0x200, &read_size, NULL);

    p_table* pt = reinterpret_cast<p_table*>(&buf[446]);
    for(int i = 0; i < 4; ++i) {
        const char* bfmes = pt[i].boot_flag & 0x80 ? "Boot" : "Boot Shinai";
        cout << "Partition " << i << " : " << bfmes << endl;
        cout << "Type : " << static_cast<DWORD>(pt[i].type) << endl;
        cout << "Start LBA : " << pt[i].start_lba << endl;
        cout << "Total Sector : " << pt[i].total_sector << endl;
    }

    CloseHandle(h);

    return 0;
}

 

----------実行結果----------

Partition 0 : Boot Shinai
Type : 4 ※ FAT16
Start LBA : 63
Total Sector : 32193
Partition 1 : Boot
Type : 7 ※ NTFS
Start LBA : 32256
Total Sector : 7773696
Partition 2 : Boot Shinai
Type : 0
Start LBA : 0
Total Sector : 0
Partition 3 : Boot Shinai
Type : 0
Start LBA : 0
Total Sector : 0

----------------------------

EeePCでは先頭パーティションに16MB程FATな何かがあって第2パーティションが起動ドライブ(Cドライブ)になってるみたい。

 

ところで、LBA領域が4バイトしかないって事はHDDが2TB超えたらMBRでは管理できなくなりますね。どうなるんだろう?

GPTとかいう規格があるらしいですけど(EFIな環境で使われてる?)、x86なWindowsではそこからは起動できないらしいし。

 

HDDの方は137GBの壁を越えるために採用されたLBA48があるから暫くの間は大丈夫でしょうが…

投稿日時 : 2008年3月27日 20:59

コメント

# re: Windowsでセクタリード 2008/03/28 2:01 通りかかり
cygwinでも読めるんだ,と少しびっくりです.
WindowsだとDisk Probeに頼りきりです・・・

CHSからLBAが主流になってましたが,今のSTATはコマンドなどが異なるんですかね?
Identify Deviceとかが懐かしい・・・

# re: Windowsでセクタリード 2008/03/28 8:13 通りかかり

誤:STAT
正:SATA

# re: Windowsでセクタリード 2008/03/28 9:24 スーパーあんどちん
>> 通りかかりさん
自分でシコシコプログラム書くくらいならDisk Probe使ったほうがいいんじゃないでしょうか^^;

Cygiwin環境ではgcc入れるとwindows用のヘッダがインストールされている(面倒くさがって全部にチェックつけたからかも)し、「Win32API使えるんだろうな。MessageBox出たし」程度の感じでやってみたら出来たってだけですが。
/usr/include/w32api
にwindows関係のヘッダは入ってるみたいです。

SATAはIDE互換モードがあるので、その場合ATA/ATAPIコマンドが使えるはずです。
AHCIの場合は全然違うでしょうけど、不勉強で知りません。
AHCIでWindowsインストールしようとするとインストール時に別途ドライバのインストールが必要になるから、IDE互換モードとは全然違った制御方法になっているのでしょうね。
Vistaだと追加ドライバ無しでインストールできるのかな?




# C#でセクタリード 2008/08/08 0:03 The beast of halfpace
C#でセクタリード

# Javaでセクタリード 2008/08/12 22:25 The beast of halfpace
Javaでセクタリード

# Javaでセクタリード 2008/08/12 22:34 The beast of halfpace
Javaでセクタリード

# re: Windowsでセクタリード 2011/03/29 16:05
//

#include <windows.h>
#include <stdio.h>

int main(void)
{

// Begin Preparations
ULARGE_INTEGER all_space;
DWORD nBytes;

HANDLE hDisk = CreateFile
(
"\\\\.\\PHYSICALDRIVE2", //Filename
GENERIC_READ, //Access mode
FILE_SHARE_READ | FILE_SHARE_WRITE, //Sharing mode
NULL, //Security info
OPEN_EXISTING, //Creating
FILE_ATTRIBUTE_NORMAL, //Attribtion
NULL //Template file handle
);

DeviceIoControl
(
hDisk,
IOCTL_DISK_GET_LENGTH_INFO,
NULL,
0,
&all_space,
sizeof(all_space),
&nBytes,
NULL
);

if(hDisk == INVALID_HANDLE_VALUE)
{
printf("Error\n");
return 1;
}

HANDLE hImage = CreateFile
(
"C:\\d.img", //Filename
GENERIC_WRITE, //Access mode
FILE_SHARE_READ | FILE_SHARE_WRITE, //Sharing mode
NULL, //Security info
CREATE_ALWAYS, //Creating
FILE_ATTRIBUTE_NORMAL, //Attribtion
NULL //Template file handle
);

if(hImage == INVALID_HANDLE_VALUE)
{
printf("Error\n");
return 1;
}

BYTE buf[512];
DWORD could_readed;
DWORD could_write;

unsigned __int64 all_sect = all_space.QuadPart / 512;
unsigned __int64 could_read = 0;
for(unsigned __int64 i = 0; i < all_sect; i++)
{
ReadFile
(
hDisk, //File handle
buf, //Buffer
512, //Filesize
&could_readed, //Read size X[B]
NULL //Over lap
);

WriteFile
(
hImage,
buf,
512,
&could_write,
NULL
);
could_read += could_readed;
printf("%I64d B / %I64d B\n", could_read, all_space.QuadPart);
}

printf("Done.");

CloseHandle(hDisk);
CloseHandle(hImage);

return 0;
}

これでDisk3のイメージファイルが作れた

# FBIEWMTDEwPOnERknE 2011/11/28 19:28 http://www.hansensurf.com/Surfboard-Wax.html
I would add something else, of course, but in fact almost everything is mentioned!...

# CGZpTrnwwDAVS 2011/12/06 17:58 http://allshoerack.com/
Comrade kill yourself.

# tGxMVcglOUhyyXGOXYD 2011/12/12 18:19 http://www.birthcontrolremedy.com/birth-control/ya
Somewhere in the Internet I have already read almost the same selection of information, but anyway thanks!!...

# UDBFRSdrukRSayrkgLB 2011/12/15 23:56 http://www.discountwatchstore.com/Bulova-Watches_c
Yeah, it is clear now !... From the very beginning I did not understand where was the connection with the title !!...

# re: Windows??????? 2021/08/07 8:55 where do you get hydroxychloroquine
chloroquine phosphate cvs https://chloroquineorigin.com/# hydroxychloroquine for malaria

# Свежие новости 2022/02/20 11:16 Adambap
Где Вы ищите свежие новости?
Лично я читаю и доверяю газете https://www.ukr.net/.
Это единственный источник свежих и независимых новостей.
Рекомендую и Вам

# write my essay for cheap h15bax 2022/09/08 18:07 Charlosmox

Cheers, I appreciate it! https://definitionessays.com/ dissertation proposal methodology

# ロレックス 時計 box 特大 2022/09/16 3:22 mdbwujm@goo.ne.jp
弊社は正規品と同等品質のコピー品を低価でお客様に提供します
コピールイヴィトン、1つのフランスの贅沢品のブランド、
最初フランスの貴族達のために鞍のブランドを作るで、
今まで着いて、依然としてハイエンドに向かう
消費者の主ななる多種の製品の贅沢品の代名詞です。
当社は日本で最高品質のコピーブランド代引き激安通販人気老舗です
ロレックス 時計 box 特大 https://www.b2kopi.com/product/detail.aspx-id=3717.htm

# Best Place to Buy Twitter accounts Real, Active & Safe 2022/12/23 17:49 HaroldLaw
Buy Bulk, PVA, Old, OG & Verified Twitter Accounts - https://sellaccs.net
Buy & Sell Twitter Accounts - cheap Bulk, Phone Verified, Old, OG, Legit, Active Twitter Account for Sale with most followers, discount price, ...


Click Home!
https://accstores.com

Thanks You!

# Mua Acc Cổ Fb 2023 ❤️Shop Tặng 275 Nick Facebook Cổ Free 2023/01/09 22:42 Raymondthype
Mua Nick Facebook Uy Tín Bán Acc Facebook Clone Tr?ng Giá R?
Lo?i c? 2009 ??n 2017, và khá hi?m nhé và giá cao h?n, tuy nhiên b?n có th? liên h? v?i shop ?? ???c bi?t t?n ...
Click Link!

http://accs.vn



# write a dissertation abstract r69hnc 2023/02/26 20:46 Robertsaids
You explained this terrifically.
phd research proposal help https://dissertationwritingtops.com/ can someone write my dissertation for me

# thesis statement for career research paper x995ct 2023/03/02 20:24 Josephbried

Regards! I value this!
thesis beginning words https://writingthesistops.com/ forming a thesis statement

# help with an essay a48wbo 2023/03/06 14:55 EugeneSib

Thanks. A lot of facts!
how to write a personal narrative essay https://essaytyperhelp.com best dissertation services https://writinganessaycollegeservice.com

# buy custom essays o92cvx 2023/03/07 6:00 EugeneSib

With thanks, I value it!
essay editing service online https://custompaperwritersservices.com please write my essay https://domyhomeworkformecheap.com

# help to write an essay m66gjj 2023/03/07 21:13 EugeneSib

Incredible lots of terrific material.
how to write great essays https://writinganessaycollegeservice.com inexpensive resume writing services https://essaywritingservicelinked.com

# essay writing assistance y39vmp 2023/03/08 12:55 EugeneSib

Cheers! An abundance of stuff!
how to write a introduction to an essay https://essaytyperhelp.com best custom essay writers https://helpwritingdissertation.com

# phd diss y17zkh 2023/03/09 5:41 EugeneSib

Very well voiced genuinely! !
phd thesis introduction https://argumentativethesis.com college admission essay editing services https://helpwithdissertationwriting.com

# writing an application essay g49jsx 2023/03/09 20:27 EugeneSib

You have made your position extremely well!!
best thesis writing service https://quality-essays.com criminology dissertation https://buycheapessaysonline.com

# how to write good college essays m729jm 2023/03/10 10:56 EugeneSib

Good material. Appreciate it.
ghostwriter needed https://service-essay.com how write a essay https://hireawriterforanessay.com

# write my essays g58bjf 2023/03/11 1:36 EugeneSib

Nicely put, Many thanks.
best medical school essay editing service https://argumentativethesis.com how to write a introduction for an essay https://essaywritinghelperonline.com

# hire writer p68mvp 2023/03/11 2:09 Gregorysaipt

Nicely put, Thanks.
instant essay writer https://hireawriterforanessay.com how to write an essay https://service-essay.com

# essays on college education r73hlj 2023/03/11 23:51 Gregorysaipt
You actually expressed this effectively!
rewriting service https://essaywritingserviceahrefs.com top 10 essay writers https://payforanessaysonline.com

# how to write time in an essay w813cv 2023/03/12 7:35 EugeneSib

Wow quite a lot of good advice!
marketing dissertations https://payforanessaysonline.com essay to write https://buyanessayscheaponline.com

# college application essay a72xqj 2023/03/12 22:07 EugeneSib

Cheers! Loads of content.
buy essay online safe https://dissertationwritingtops.com prompts for college essays https://quality-essays.com

# write an opinion essay x50ybq 2023/03/31 20:20 EugeneSib

Kudos. Excellent information.
successful college essays https://writinganessaycollegeservice.com why i want to attend college essay https://essayservicehelp.com

# doctorate paper k731hj 2023/04/03 7:23 EugeneSib

Lovely information, Thanks a lot.
essay writing checker https://bestpaperwritingservice.com reliable essay writing service https://service-essay.com

# The plugins developed for WordPress 2023/05/09 23:16 Justas
The plugins developed for WordPress serve to enhance the features and functions of a WordPress website, allowing you to build your awesome and functional site https://t.me/wpigaming/648 Customise WordPress with powerful, professional and intuitive fields.

# generic for valtrex https://valtrex.auction/ valtrex generic 2023/10/25 1:41 Valtrex
generic for valtrex https://valtrex.auction/ valtrex generic

# best ed pills at gnc https://edpills.tech/# best ed drug 2023/12/23 11:03 EdPills
best ed pills at gnc https://edpills.tech/# best ed drug

Post Feedback

タイトル
名前
Url:
コメント: