using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Runtime.InteropServices; using System.Text; using Aaru.CommonTypes; using Aaru.CommonTypes.AaruMetadata; using Aaru.CommonTypes.Enums; using Aaru.CommonTypes.Interfaces; using Aaru.CommonTypes.Structs; using Aaru.Helpers; using Marshal = Aaru.Helpers.Marshal; namespace Aaru.DiscImages; public class GameBoyAdvance : IByteAddressableImage { byte[] _data; Stream _dataStream; ImageInfo _imageInfo; bool _opened; #region IByteAddressableImage Members /// public string Author => Authors.NataliaPortillo; /// public Metadata AaruMetadata => null; /// public List DumpHardware => null; /// public string Format => "Nintendo Game Boy Advance cartridge dump"; /// public Guid Id => new("0040DDEB-3902-4402-9028-62915C5AA81F"); /// public ImageInfo Info => _imageInfo; /// public string Name => Localization.GameBoyAdvance_Name; /// public bool Identify(IFilter imageFilter) { if(imageFilter == null) return false; Stream stream = imageFilter.GetDataForkStream(); // Not sure but seems to be a multiple of at least this if(stream.Length % 32768 != 0) return false; stream.Position = 4; var magicBytes = new byte[8]; stream.EnsureRead(magicBytes, 0, 8); var magic = BitConverter.ToUInt64(magicBytes, 0); return magic == 0x21A29A6951AEFF24; } /// public ErrorNumber Open(IFilter imageFilter) { if(imageFilter == null) return ErrorNumber.NoSuchFile; Stream stream = imageFilter.GetDataForkStream(); // Not sure but seems to be a multiple of at least this, maybe more if(stream.Length % 512 != 0) return ErrorNumber.InvalidArgument; stream.Position = 4; var magicBytes = new byte[8]; stream.EnsureRead(magicBytes, 0, 8); var magic = BitConverter.ToUInt64(magicBytes, 0); if(magic != 0x21A29A6951AEFF24) return ErrorNumber.InvalidArgument; _data = new byte[imageFilter.DataForkLength]; stream.Position = 0; stream.EnsureRead(_data, 0, (int)imageFilter.DataForkLength); _imageInfo = new ImageInfo { CreationTime = imageFilter.CreationTime, ImageSize = (ulong)imageFilter.DataForkLength, MediaType = MediaType.GameBoyAdvanceGamePak, LastModificationTime = imageFilter.LastWriteTime, Sectors = (ulong)imageFilter.DataForkLength, MetadataMediaType = MetadataMediaType.LinearMedia }; Header header = Marshal.ByteArrayToStructureBigEndian
(_data, 0, Marshal.SizeOf
()); _imageInfo.MediaTitle = StringHandlers.CToString(header.Name); var sb = new StringBuilder(); sb.AppendFormat(Localization.Name_0, _imageInfo.MediaTitle).AppendLine(); sb.AppendFormat(Localization.Device_type_0, header.DeviceType).AppendLine(); sb.AppendFormat(Localization.Console_type_0, header.UnitCode).AppendLine(); sb.AppendFormat(Localization.Product_code_AGB_0, StringHandlers.CToString(header.Code)).AppendLine(); sb.AppendFormat(Localization.Maker_code_0, StringHandlers.CToString(header.Maker)).AppendLine(); sb.AppendFormat(Localization.Revision_0, header.Revision).AppendLine(); sb.AppendFormat(Localization.Header_checksum_0, header.ComplementCheck).AppendLine(); _imageInfo.Comments = sb.ToString(); _opened = true; return ErrorNumber.NoError; } /// public string ErrorMessage { get; private set; } /// public bool IsWriting { get; private set; } /// public IEnumerable KnownExtensions => new[] { ".gba" }; /// public IEnumerable SupportedMediaTags => Array.Empty(); /// public IEnumerable SupportedMediaTypes => new[] { MediaType.GameBoyAdvanceGamePak }; /// public IEnumerable<(string name, Type type, string description, object @default)> SupportedOptions => Array.Empty<(string name, Type type, string description, object @default)>(); /// public IEnumerable SupportedSectorTags => Array.Empty(); /// public bool Create(string path, MediaType mediaType, Dictionary options, ulong sectors, uint sectorSize) => Create(path, mediaType, options, (long)sectors) == ErrorNumber.NoError; /// public bool Close() { if(!_opened) { ErrorMessage = Localization.No_image_has_been_opened; return false; } if(!IsWriting) { ErrorMessage = Localization.Image_is_not_opened_for_writing; return false; } _dataStream.Position = 0; _dataStream.Write(_data, 0, _data.Length); _dataStream.Close(); IsWriting = false; _opened = false; return true; } /// public bool SetMetadata(Metadata metadata) => false; /// public bool SetDumpHardware(List dumpHardware) => false; /// public bool SetImageInfo(ImageInfo imageInfo) => true; /// public long Position { get; set; } /// public ErrorNumber Create(string path, MediaType mediaType, Dictionary options, long maximumSize) { if(_opened) { ErrorMessage = Localization.Cannot_create_an_opened_image; return ErrorNumber.InvalidArgument; } if(mediaType != MediaType.GameBoyAdvanceGamePak) { ErrorMessage = string.Format(Localization.Unsupported_media_format_0, mediaType); return ErrorNumber.NotSupported; } _imageInfo = new ImageInfo { MediaType = mediaType, Sectors = (ulong)maximumSize }; try { _dataStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); } catch(IOException e) { ErrorMessage = string.Format(Localization.Could_not_create_new_image_file_exception_0, e.Message); return ErrorNumber.InOutError; } _imageInfo.MediaType = mediaType; IsWriting = true; _opened = true; _data = new byte[maximumSize]; return ErrorNumber.NoError; } /// public ErrorNumber GetMappings(out LinearMemoryMap mappings) { mappings = new LinearMemoryMap(); if(!_opened) { ErrorMessage = Localization.No_image_has_been_opened; return ErrorNumber.NotOpened; } mappings = new LinearMemoryMap { Devices = new[] { new LinearMemoryDevice { Type = LinearMemoryType.ROM, PhysicalAddress = new LinearMemoryAddressing { Start = 0, Length = (ulong)_data.Length } } } }; return ErrorNumber.NoError; } /// public ErrorNumber ReadByte(out byte b, bool advance = true) => ReadByteAt(Position, out b, advance); /// public ErrorNumber ReadByteAt(long position, out byte b, bool advance = true) { b = 0; if(!_opened) { ErrorMessage = Localization.No_image_has_been_opened; return ErrorNumber.NotOpened; } if(position >= _data.Length) { ErrorMessage = Localization.The_requested_position_is_out_of_range; return ErrorNumber.OutOfRange; } b = _data[position]; if(advance) Position = position + 1; return ErrorNumber.NoError; } /// public ErrorNumber ReadBytes(byte[] buffer, int offset, int bytesToRead, out int bytesRead, bool advance = true) => ReadBytesAt(Position, buffer, offset, bytesToRead, out bytesRead, advance); /// public ErrorNumber ReadBytesAt(long position, byte[] buffer, int offset, int bytesToRead, out int bytesRead, bool advance = true) { bytesRead = 0; if(!_opened) { ErrorMessage = Localization.No_image_has_been_opened; return ErrorNumber.NotOpened; } if(position >= _data.Length) { ErrorMessage = Localization.The_requested_position_is_out_of_range; return ErrorNumber.OutOfRange; } if(buffer is null) { ErrorMessage = Localization.Buffer_must_not_be_null; return ErrorNumber.InvalidArgument; } if(offset + bytesToRead > buffer.Length) bytesRead = buffer.Length - offset; if(position + bytesToRead > _data.Length) bytesToRead = (int)(_data.Length - position); Array.Copy(_data, position, buffer, offset, bytesToRead); if(advance) Position = position + bytesToRead; bytesRead = bytesToRead; return ErrorNumber.NoError; } /// public ErrorNumber SetMappings(LinearMemoryMap mappings) { if(!_opened) { ErrorMessage = Localization.No_image_has_been_opened; return ErrorNumber.NotOpened; } if(!IsWriting) { ErrorMessage = Localization.Image_is_not_opened_for_writing; return ErrorNumber.ReadOnly; } var foundRom = false; var foundSaveRam = false; // Sanitize foreach(LinearMemoryDevice map in mappings.Devices) { switch(map.Type) { case LinearMemoryType.ROM when !foundRom: foundRom = true; break; case LinearMemoryType.SaveRAM when !foundSaveRam: foundSaveRam = true; break; default: return ErrorNumber.InvalidArgument; } } // Cannot save in this image format anyway return foundRom ? ErrorNumber.NoError : ErrorNumber.InvalidArgument; } /// public ErrorNumber WriteByte(byte b, bool advance = true) => WriteByteAt(Position, b, advance); /// public ErrorNumber WriteByteAt(long position, byte b, bool advance = true) { if(!_opened) { ErrorMessage = Localization.No_image_has_been_opened; return ErrorNumber.NotOpened; } if(!IsWriting) { ErrorMessage = Localization.Image_is_not_opened_for_writing; return ErrorNumber.ReadOnly; } if(position >= _data.Length) { ErrorMessage = Localization.The_requested_position_is_out_of_range; return ErrorNumber.OutOfRange; } _data[position] = b; if(advance) Position = position + 1; return ErrorNumber.NoError; } /// public ErrorNumber WriteBytes(byte[] buffer, int offset, int bytesToWrite, out int bytesWritten, bool advance = true) => WriteBytesAt(Position, buffer, offset, bytesToWrite, out bytesWritten, advance); /// public ErrorNumber WriteBytesAt(long position, byte[] buffer, int offset, int bytesToWrite, out int bytesWritten, bool advance = true) { bytesWritten = 0; if(!_opened) { ErrorMessage = Localization.No_image_has_been_opened; return ErrorNumber.NotOpened; } if(!IsWriting) { ErrorMessage = Localization.Image_is_not_opened_for_writing; return ErrorNumber.ReadOnly; } if(position >= _data.Length) { ErrorMessage = Localization.The_requested_position_is_out_of_range; return ErrorNumber.OutOfRange; } if(buffer is null) { ErrorMessage = Localization.Buffer_must_not_be_null; return ErrorNumber.InvalidArgument; } if(offset + bytesToWrite > buffer.Length) bytesToWrite = buffer.Length - offset; if(position + bytesToWrite > _data.Length) bytesToWrite = (int)(_data.Length - position); Array.Copy(buffer, offset, _data, position, bytesToWrite); if(advance) Position = position + bytesToWrite; bytesWritten = bytesToWrite; return ErrorNumber.NoError; } #endregion #region Nested type: Header [StructLayout(LayoutKind.Sequential, Pack = 1)] [SuppressMessage("ReSharper", "MemberCanBePrivate.Local")] [SuppressMessage("ReSharper", "FieldCanBeMadeReadOnly.Local")] struct Header { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public byte[] EntryPoint; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 156)] public byte[] Logo; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)] public byte[] Name; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public byte[] Code; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] public byte[] Maker; public byte Fixed; public byte UnitCode; public byte DeviceType; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)] public byte[] Reserved; public byte Revision; public byte ComplementCheck; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] public byte[] Reserved2; } #endregion }