📄 rtfbs.c
字号:
/*****************************************************************************
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of MediaTek Inc. (C) 2005
*
* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/
/*****************************************************************************
*
* Filename:
* ---------
* rtfbs.c
*
* Project:
* --------
* Maui
*
* Description:
* ------------
* This file provides boot sector initialization
*
* Author:
* -------
*
*
*============================================================================
* HISTORY
* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*------------------------------------------------------------------------------
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
*
*------------------------------------------------------------------------------
* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*============================================================================
****************************************************************************/
/* RTFiles boot sector initalization. */
#include <stdlib.h>
#include <string.h>
#include "kal_release.h"
#include <rtfiles.h>
#include "fs_kal.h"
#define MAX_CLUSTERS_16 (0xFFF6 - 2) // max number of clusters on a FAT-16 partition
#define MAX_CLUSTERS_12 (0x0FF6 - 2) // max number of clusters on a FAT-12 partition
#define SECTOR_SIZE 512
#define STARTTRACK(P) (P->StartTrack | ((P->StartSector & 0xC0) << 2))
#define ENDTRACK(P) (P->EndTrack | ((P->EndSector & 0xC0) << 2))
static const BYTE MasterBootCode[] = {
0x33, 0xC0, 0x8E, 0xD0, 0xBC, 0x00, 0x7C, 0xFB, 0x50, 0x07, 0x50, 0x1F, 0x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08, 0x13, 0x56, 0x0A, 0xE8, 0x12, 0x00, 0x5A, 0xEB,
0xD5, 0x4F, 0x74, 0xE4, 0x33, 0xC0, 0xCD, 0x13, 0xEB, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x56, 0x33, 0xF6, 0x56, 0x56, 0x52, 0x50, 0x06, 0x53, 0x51, 0xBE, 0x10, 0x00, 0x56, 0x8B, 0xF4,
0x50, 0x52, 0xB8, 0x00, 0x42, 0x8A, 0x56, 0x24, 0xCD, 0x13, 0x5A, 0x58, 0x8D, 0x64, 0x10, 0x72,
0x0A, 0x40, 0x75, 0x01, 0x42, 0x80, 0xC7, 0x02, 0xE2, 0xF7, 0xF8, 0x5E, 0xC3, 0xEB, 0x74, 0x49,
0x6E, 0x76, 0x61, 0x6C, 0x69, 0x64, 0x20, 0x70, 0x61, 0x72, 0x74, 0x69, 0x74, 0x69, 0x6F, 0x6E,
/* p a r t i t i o n */
0x20, 0x74, 0x61, 0x62, 0x6C, 0x65, 0x00, 0x45, 0x72, 0x72, 0x6F, 0x72, 0x20, 0x6C, 0x6F, 0x61,
/* SP t a b l e NIL e r r o r SP l o a */
0x64, 0x69, 0x6E, 0x67, 0x20, 0x6F, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6E, 0x67, 0x20, 0x73,
/* d i n g SP o p e r a t i n g SP s */
0x79, 0x73, 0x74, 0x65, 0x6D, 0x00, 0x4D, 0x69, 0x73, 0x73, 0x69, 0x6E, 0x67, 0x20, 0x6F, 0x70,
/* y s t e m NIL M i s s i n g SP o p */
0x65, 0x72, 0x61, 0x74, 0x69, 0x6E, 0x67, 0x20, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x00, 0x00,
/* e r a t i n g SP s y s t e m */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x8B, 0xFC, 0x1E, 0x57, 0x8B, 0xF5, 0xCB, 0x00};
static const BYTE BootCode[] = {
0xFA, 0xB8, 0xC0, 0x07, 0x8E, 0xD8, 0x8E, 0xD0, 0xBC, 0x00, 0x04, 0xFB, 0xBE, 0x83, 0x00, 0xE8,
0x0C, 0x00, 0xCD, 0x18, 0xF4, 0xEB, 0xFD, 0xB4, 0x0E, 0x33, 0xDB, 0xCD, 0x10, 0xC3, 0xAC, 0x0A,
0xC0, 0x74, 0x05, 0xE8, 0xF1, 0xFF, 0xEB, 0xF6, 0xC3,
//File System Boot Sector (C) is reserved.
0x46, 0x69, 0x6c, 0x65, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x20, 0x42, 0x6F, 0x6F, 0x74,
0x20, 0x53, 0x65, 0x63, 0x74, 0x6F, 0x72, 0x20, 0x28, 0x43, 0x29, 0x20, 0x69, 0x73, 0x20, 0x72,
0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x64, 0x2E,
//RTFiles Boot sector (c) 1998, 99 On Time
//0x52, 0x54, 0x46, 0x69, 0x6C, 0x65, 0x73, 0x20, 0x42, 0x6F, 0x6F, 0x74, 0x20, 0x53, 0x65, 0x63,
//0x74, 0x6F, 0x72, 0x20, 0x20, 0x28, 0x63, 0x29, 0x20, 0x31, 0x39, 0x39, 0x38, 0x2C, 0x39, 0x39,
//0x20, 0x4F, 0x6E, 0x20, 0x54, 0x69, 0x6D, 0x65,
0x0A, 0x0D, 0x54, 0x68, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F, 0x20, 0x4F, 0x53,
/* LF CR T h e r e SP i s SP n o SP o s */
0x20, 0x74, 0x6F, 0x20, 0x62, 0x6F, 0x6F, 0x74, 0x20, 0x6F, 0x6E, 0x20, 0x74, 0x68, 0x69, 0x73,
/* SP t o SP b o o t SP o n SP t h i s */
0x20, 0x64, 0x69, 0x73, 0x6B, 0x00};
/* SP d i s k NIL */
/*----------------------------------*/
static int QPowerTwo(unsigned int i)
{
while (i != 0)
{
if ((i & 1) && (i != 1))
return 0;
i >>= 1;
}
return 1;
}
/*-----------------------------------*/
BYTE DefaultOSType(DWORD Sectors, DWORD LastCylinder)
{
#define M *2048l
BYTE Result;
if (Sectors < (16 M))
Result = 0x01; // FAT 12
else if (Sectors < (32 M))
Result = 0x04; // FAT 16, DOS < 4.0
else if (Sectors < (512 M))
Result = 0x06; // FAT 16, DOS >= 4.0
else
Result = 0x0B; // FAT 32
if (LastCylinder >= 1024)
switch (Result)
{
case 0x01:
case 0x04:
case 0x06:
Result = 0x0E; // FAT 16 LBA
break;
case 0x0B:
Result = 0x0C; // FAT 32 LBA
break;
}
return Result;
}
/*-----------------------------------*/
int RTFAPI RTFCreateMasterBootRecord(void * MasterBootRecord,
const RTFPartitionRecord * DiskGeometry)
{
RTFMasterBootRecord * MBR = MasterBootRecord;
DWORD EndHead, EndTrack, Sectors;
EndHead = DiskGeometry->EndHead;
Sectors = DiskGeometry->EndSector & 63;
EndTrack = DiskGeometry->EndTrack | (((DiskGeometry->EndSector & 0xC0) << 2));
if (DiskGeometry->Sectors == 0)
return RTF_PARAM_ERROR;
if (DiskGeometry->Sectors < (1l * ((EndHead+1) * Sectors * (EndTrack+1))))
return RTF_PARAM_ERROR;
if (DiskGeometry->Sectors < 5)
return RTF_PARAM_ERROR;
// If Sectors is zero, then we have to make up values for EndHead, Sectors, and EndTrack
// Since partitions always occupy complete tracks, try to minimize Sectors.
// Some systems will grab a complete cylinder, so keep Heads low too.
// A maximum CHS partition has (256*63*1024) sectors.
if (Sectors == 0)
{
Sectors = (DiskGeometry->Sectors-1) / (256l * 1 * 1024) + 1;
Sectors = min(63, Sectors);
EndHead = (DiskGeometry->Sectors-1) / (1l * Sectors * 1024);
EndHead = min(255, EndHead);
EndTrack = (DiskGeometry->Sectors-1) / ((EndHead+1) * Sectors * 1);
if (EndTrack >= 1024) // we can't handle such tracks
EndTrack = 0;
}
// check if the geometry can be used to access all of the drive
// if not, do CHS translation
while (DiskGeometry->Sectors > (1l * ((EndHead+1) * Sectors * (EndTrack+1))))
{
DWORD Heads = EndHead+1;
if (Heads <= 128)
{
Heads *= 2;
EndHead = Heads - 1;
EndTrack = (DiskGeometry->Sectors-1) / (Heads * Sectors * 1);
if (EndTrack >= 1024) // we can't handle such tracks
EndTrack = 0;
}
else
break;
}
memset((void *)MBR, 0, SECTOR_SIZE);
memcpy((void *)MBR->BootCode, (void *)MasterBootCode, sizeof( MasterBootCode));
MBR->Signature = 0xAA55;
MBR->PTable[0].BootIndicator = 0x80;
// we will reserve the first track (not cylinder) for the partition table
// CHS stuff. These fields are ignored on LBA-capable systems
MBR->PTable[0].StartHead = EndHead ? 1 : 0;
MBR->PTable[0].StartSector = 1;
MBR->PTable[0].StartTrack = EndHead ? 0 : 1;
MBR->PTable[0].EndHead = EndHead;
MBR->PTable[0].EndSector = Sectors | ((EndTrack & 0x300) >> 2);
MBR->PTable[0].EndTrack = EndTrack;
// LBA stuff. This must be correct on any size disk
MBR->PTable[0].RelativeSector = Sectors;
MBR->PTable[0].Sectors = DiskGeometry->Sectors - Sectors;
MBR->PTable[0].OSType = DefaultOSType(MBR->PTable[0].Sectors, (DiskGeometry->Sectors-1) / ((EndHead+1)*Sectors));
return RTF_NO_ERROR;
}
/*-----------------------------------*/
int RTFAPI RTFSplitPartition(void * MasterBootRecord, RTFSector Sectors)
{
RTFMasterBootRecord * MBR = MasterBootRecord;
DWORD SectorsPerCylinder,
NewStartSector,
NewStartCylinder,
OldEndCylinder;
RTFPartitionRecord * P = NULL; // The partition to split
int i;
// find the last partition and make sure an empty one follows
for (i=0; i<4; i++)
if (MBR->PTable[i].OSType != 0)
P = MBR->PTable + i;
if (P == NULL)
return RTF_CORRUPTED_PARTITION_TABLE;
if (P > (MBR->PTable + 2))
return RTF_INVALID_FILE_SYSTEM;
SectorsPerCylinder = (P->EndHead + 1) * (P->EndSector & 63);
// round Sectors such that the end of the partition will be on a cylinder
// boundary
NewStartCylinder = (P->RelativeSector + Sectors) / SectorsPerCylinder;
NewStartSector = NewStartCylinder * SectorsPerCylinder;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -