1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
|
// Copyright 2015 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <fmt/format.h>
#include "common/alignment.h"
#include "common/archives.h"
#include "common/common_paths.h"
#include "common/file_util.h"
#include "common/logging/log.h"
#include "common/string_util.h"
#include "core/core.h"
#include "core/file_sys/errors.h"
#include "core/file_sys/ncch_container.h"
#include "core/file_sys/title_metadata.h"
#include "core/hle/ipc_helpers.h"
#include "core/hle/kernel/client_session.h"
#include "core/hle/kernel/errors.h"
#include "core/hle/kernel/server_session.h"
#include "core/hle/kernel/session.h"
#include "core/hle/service/am/am.h"
#include "core/hle/service/am/am_app.h"
#include "core/hle/service/am/am_net.h"
#include "core/hle/service/am/am_sys.h"
#include "core/hle/service/am/am_u.h"
#include "core/hle/service/fs/archive.h"
#include "core/hle/service/fs/fs_user.h"
#include "core/loader/loader.h"
#include "core/loader/smdh.h"
#include "core/nus_download.h"
SERIALIZE_EXPORT_IMPL(Service::AM::Module)
SERVICE_CONSTRUCT_IMPL(Service::AM::Module)
namespace Service::AM {
constexpr u16 PLATFORM_CTR = 0x0004;
constexpr u16 CATEGORY_SYSTEM = 0x0010;
constexpr u16 CATEGORY_DLP = 0x0001;
constexpr u8 VARIATION_SYSTEM = 0x02;
constexpr u32 TID_HIGH_UPDATE = 0x0004000E;
constexpr u32 TID_HIGH_DLC = 0x0004008C;
struct TitleInfo {
u64_le tid;
u64_le size;
u16_le version;
u16_le unused;
u32_le type;
};
static_assert(sizeof(TitleInfo) == 0x18, "Title info structure size is wrong");
constexpr u8 OWNERSHIP_DOWNLOADED = 0x01;
constexpr u8 OWNERSHIP_OWNED = 0x02;
struct ContentInfo {
u16_le index;
u16_le type;
u32_le content_id;
u64_le size;
u8 ownership;
INSERT_PADDING_BYTES(0x7);
};
static_assert(sizeof(ContentInfo) == 0x18, "Content info structure size is wrong");
struct TicketInfo {
u64_le title_id;
u64_le ticket_id;
u16_le version;
u16_le unused;
u32_le size;
};
static_assert(sizeof(TicketInfo) == 0x18, "Ticket info structure size is wrong");
class CIAFile::DecryptionState {
public:
std::vector<CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption> content;
};
CIAFile::CIAFile(Core::System& system_, Service::FS::MediaType media_type)
: system(system_), media_type(media_type),
decryption_state(std::make_unique<DecryptionState>()) {}
CIAFile::~CIAFile() {
Close();
}
ResultVal<std::size_t> CIAFile::Read(u64 offset, std::size_t length, u8* buffer) const {
UNIMPLEMENTED();
return length;
}
Result CIAFile::WriteTicket() {
auto load_result = container.LoadTicket(data, container.GetTicketOffset());
if (load_result != Loader::ResultStatus::Success) {
LOG_ERROR(Service_AM, "Could not read ticket from CIA.");
// TODO: Correct result code.
return {ErrCodes::InvalidCIAHeader, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Permanent};
}
// TODO: Write out .tik files to nand?
install_state = CIAInstallState::TicketLoaded;
return ResultSuccess;
}
Result CIAFile::WriteTitleMetadata() {
auto load_result = container.LoadTitleMetadata(data, container.GetTitleMetadataOffset());
if (load_result != Loader::ResultStatus::Success) {
LOG_ERROR(Service_AM, "Could not read title metadata from CIA.");
// TODO: Correct result code.
return {ErrCodes::InvalidCIAHeader, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Permanent};
}
FileSys::TitleMetadata tmd = container.GetTitleMetadata();
tmd.Print();
// If a TMD already exists for this app (ie 00000000.tmd), the incoming TMD
// will be the same plus one, (ie 00000001.tmd), both will be kept until
// the install is finalized and old contents can be discarded.
if (FileUtil::Exists(GetTitleMetadataPath(media_type, tmd.GetTitleID()))) {
is_update = true;
}
std::string tmd_path = GetTitleMetadataPath(media_type, tmd.GetTitleID(), is_update);
// Create content/ folder if it doesn't exist
std::string tmd_folder;
Common::SplitPath(tmd_path, &tmd_folder, nullptr, nullptr);
FileUtil::CreateFullPath(tmd_folder);
// Save TMD so that we can start getting new .app paths
if (tmd.Save(tmd_path) != Loader::ResultStatus::Success) {
LOG_ERROR(Service_AM, "Failed to install title metadata file from CIA.");
// TODO: Correct result code.
return FileSys::ResultFileNotFound;
}
// Create any other .app folders which may not exist yet
std::string app_folder;
auto main_content_path = GetTitleContentPath(media_type, tmd.GetTitleID(),
FileSys::TMDContentIndex::Main, is_update);
Common::SplitPath(main_content_path, &app_folder, nullptr, nullptr);
FileUtil::CreateFullPath(app_folder);
auto content_count = container.GetTitleMetadata().GetContentCount();
content_written.resize(content_count);
content_files.clear();
for (std::size_t i = 0; i < content_count; i++) {
auto path = GetTitleContentPath(media_type, tmd.GetTitleID(), i, is_update);
auto& file = content_files.emplace_back(path, "wb");
if (!file.IsOpen()) {
LOG_ERROR(Service_AM, "Could not open output file '{}' for content {}.", path, i);
// TODO: Correct error code.
return FileSys::ResultFileNotFound;
}
}
if (container.GetTitleMetadata().HasEncryptedContent()) {
if (auto title_key = container.GetTicket().GetTitleKey()) {
decryption_state->content.resize(content_count);
for (std::size_t i = 0; i < content_count; ++i) {
auto ctr = tmd.GetContentCTRByIndex(i);
decryption_state->content[i].SetKeyWithIV(title_key->data(), title_key->size(),
ctr.data());
}
} else {
LOG_ERROR(Service_AM, "Could not read title key from ticket for encrypted CIA.");
// TODO: Correct error code.
return FileSys::ResultFileNotFound;
}
} else {
LOG_INFO(Service_AM,
"Title has no encrypted content, skipping initializing decryption state.");
}
install_state = CIAInstallState::TMDLoaded;
return ResultSuccess;
}
ResultVal<std::size_t> CIAFile::WriteContentData(u64 offset, std::size_t length, const u8* buffer) {
// Data is not being buffered, so we have to keep track of how much of each <ID>.app
// has been written since we might get a written buffer which contains multiple .app
// contents or only part of a larger .app's contents.
const u64 offset_max = offset + length;
for (std::size_t i = 0; i < content_written.size(); i++) {
if (content_written[i] < container.GetContentSize(i)) {
// The size, minimum unwritten offset, and maximum unwritten offset of this content
const u64 size = container.GetContentSize(i);
const u64 range_min = container.GetContentOffset(i) + content_written[i];
const u64 range_max = container.GetContentOffset(i) + size;
// The unwritten range for this content is beyond the buffered data we have
// or comes before the buffered data we have, so skip this content ID.
if (range_min > offset_max || range_max < offset) {
continue;
}
// Figure out how much of this content ID we have just recieved/can write out
const u64 available_to_write = std::min(offset_max, range_max) - range_min;
// Since the incoming TMD has already been written, we can use GetTitleContentPath
// to get the content paths to write to.
FileSys::TitleMetadata tmd = container.GetTitleMetadata();
auto& file = content_files[i];
std::vector<u8> temp(buffer + (range_min - offset),
buffer + (range_min - offset) + available_to_write);
if ((tmd.GetContentTypeByIndex(i) & FileSys::TMDContentTypeFlag::Encrypted) != 0) {
decryption_state->content[i].ProcessData(temp.data(), temp.data(), temp.size());
}
file.WriteBytes(temp.data(), temp.size());
// Keep tabs on how much of this content ID has been written so new range_min
// values can be calculated.
content_written[i] += available_to_write;
LOG_DEBUG(Service_AM, "Wrote {:x} to content {}, total {:x}", available_to_write, i,
content_written[i]);
}
}
return length;
}
ResultVal<std::size_t> CIAFile::Write(u64 offset, std::size_t length, bool flush,
const u8* buffer) {
written += length;
// TODO(shinyquagsire23): Can we assume that things will only be written in sequence?
// Does AM send an error if we write to things out of order?
// Or does it just ignore offsets and assume a set sequence of incoming data?
// The data in CIAs is always stored CIA Header > Cert > Ticket > TMD > Content > Meta.
// The CIA Header describes Cert, Ticket, TMD, total content sizes, and TMD is needed for
// content sizes so it ends up becoming a problem of keeping track of how much has been
// written and what we have been able to pick up.
if (install_state == CIAInstallState::InstallStarted) {
std::size_t buf_copy_size = std::min(length, FileSys::CIA_HEADER_SIZE);
std::size_t buf_max_size =
std::min(static_cast<std::size_t>(offset + length), FileSys::CIA_HEADER_SIZE);
data.resize(buf_max_size);
std::memcpy(data.data() + offset, buffer, buf_copy_size);
// We have enough data to load a CIA header and parse it.
if (written >= FileSys::CIA_HEADER_SIZE) {
container.LoadHeader(data);
container.Print();
install_state = CIAInstallState::HeaderLoaded;
}
}
// If we don't have a header yet, we can't pull offsets of other sections
if (install_state == CIAInstallState::InstallStarted) {
return length;
}
// If we have been given data before (or including) .app content, pull it into
// our buffer, but only pull *up to* the content offset, no further.
if (offset < container.GetContentOffset()) {
std::size_t buf_loaded = data.size();
std::size_t copy_offset = std::max(static_cast<std::size_t>(offset), buf_loaded);
std::size_t buf_offset = buf_loaded - offset;
std::size_t buf_copy_size =
std::min(length, static_cast<std::size_t>(container.GetContentOffset() - offset)) -
buf_offset;
std::size_t buf_max_size = std::min(offset + length, container.GetContentOffset());
data.resize(buf_max_size);
std::memcpy(data.data() + copy_offset, buffer + buf_offset, buf_copy_size);
}
// The end of our TMD is at the beginning of Content data, so ensure we have that much
// buffered before trying to parse.
if (written >= container.GetContentOffset() && install_state != CIAInstallState::TMDLoaded) {
auto result = WriteTicket();
if (result.IsError()) {
return result;
}
result = WriteTitleMetadata();
if (result.IsError()) {
return result;
}
}
// Content data sizes can only be retrieved from TMD data
if (install_state != CIAInstallState::TMDLoaded) {
return length;
}
// From this point forward, data will no longer be buffered in data
auto result = WriteContentData(offset, length, buffer);
if (result.Failed()) {
return result;
}
return length;
}
u64 CIAFile::GetSize() const {
return written;
}
bool CIAFile::SetSize(u64 size) const {
return false;
}
bool CIAFile::Close() const {
bool complete =
install_state >= CIAInstallState::TMDLoaded &&
content_written.size() == container.GetTitleMetadata().GetContentCount() &&
std::all_of(content_written.begin(), content_written.end(),
[this, i = 0](auto& bytes_written) mutable {
return bytes_written >= container.GetContentSize(static_cast<u16>(i++));
});
// Install aborted
if (!complete) {
LOG_ERROR(Service_AM, "CIAFile closed prematurely, aborting install...");
FileUtil::DeleteDir(GetTitlePath(media_type, container.GetTitleMetadata().GetTitleID()));
return true;
}
// Clean up older content data if we installed newer content on top
std::string old_tmd_path =
GetTitleMetadataPath(media_type, container.GetTitleMetadata().GetTitleID(), false);
std::string new_tmd_path =
GetTitleMetadataPath(media_type, container.GetTitleMetadata().GetTitleID(), true);
if (FileUtil::Exists(new_tmd_path) && old_tmd_path != new_tmd_path) {
FileSys::TitleMetadata old_tmd;
FileSys::TitleMetadata new_tmd;
old_tmd.Load(old_tmd_path);
new_tmd.Load(new_tmd_path);
// For each content ID in the old TMD, check if there is a matching ID in the new
// TMD. If a CIA contains (and wrote to) an identical ID, it should be kept while
// IDs which only existed for the old TMD should be deleted.
for (std::size_t old_index = 0; old_index < old_tmd.GetContentCount(); old_index++) {
bool abort = false;
for (std::size_t new_index = 0; new_index < new_tmd.GetContentCount(); new_index++) {
if (old_tmd.GetContentIDByIndex(old_index) ==
new_tmd.GetContentIDByIndex(new_index)) {
abort = true;
}
}
if (abort) {
break;
}
// If the file to delete is the current launched rom, signal the system to delete
// the current rom instead of deleting it now, once all the handles to the file
// are closed.
std::string to_delete =
GetTitleContentPath(media_type, old_tmd.GetTitleID(), old_index);
if (!system.IsPoweredOn() || !system.SetSelfDelete(to_delete)) {
FileUtil::Delete(to_delete);
}
}
FileUtil::Delete(old_tmd_path);
}
return true;
}
void CIAFile::Flush() const {}
TicketFile::TicketFile() {}
TicketFile::~TicketFile() {
Close();
}
ResultVal<std::size_t> TicketFile::Read(u64 offset, std::size_t length, u8* buffer) const {
UNIMPLEMENTED();
return length;
}
ResultVal<std::size_t> TicketFile::Write(u64 offset, std::size_t length, bool flush,
const u8* buffer) {
written += length;
data.resize(written);
std::memcpy(data.data() + offset, buffer, length);
return length;
}
u64 TicketFile::GetSize() const {
return written;
}
bool TicketFile::SetSize(u64 size) const {
return false;
}
bool TicketFile::Close() const {
FileSys::Ticket ticket;
if (ticket.Load(data, 0) == Loader::ResultStatus::Success) {
LOG_WARNING(Service_AM, "Discarding ticket for {:#016X}.", ticket.GetTitleID());
} else {
LOG_ERROR(Service_AM, "Invalid ticket provided to TicketFile.");
}
return true;
}
void TicketFile::Flush() const {}
InstallStatus InstallCIA(const std::string& path,
std::function<ProgressCallback>&& update_callback) {
LOG_INFO(Service_AM, "Installing {}...", path);
if (!FileUtil::Exists(path)) {
LOG_ERROR(Service_AM, "File {} does not exist!", path);
return InstallStatus::ErrorFileNotFound;
}
FileSys::CIAContainer container;
if (container.Load(path) == Loader::ResultStatus::Success) {
Service::AM::CIAFile installFile(
Core::System::GetInstance(),
Service::AM::GetTitleMediaType(container.GetTitleMetadata().GetTitleID()));
bool title_key_available = container.GetTicket().GetTitleKey().has_value();
if (!title_key_available && container.GetTitleMetadata().HasEncryptedContent()) {
LOG_ERROR(Service_AM, "File {} is encrypted and no title key is available! Aborting...",
path);
return InstallStatus::ErrorEncrypted;
}
FileUtil::IOFile file(path, "rb");
if (!file.IsOpen()) {
LOG_ERROR(Service_AM, "Could not open CIA file '{}'.", path);
return InstallStatus::ErrorFailedToOpenFile;
}
std::array<u8, 0x10000> buffer;
auto file_size = file.GetSize();
std::size_t total_bytes_read = 0;
while (total_bytes_read != file_size) {
std::size_t bytes_read = file.ReadBytes(buffer.data(), buffer.size());
auto result = installFile.Write(static_cast<u64>(total_bytes_read), bytes_read, true,
static_cast<u8*>(buffer.data()));
if (update_callback) {
update_callback(total_bytes_read, file_size);
}
if (result.Failed()) {
LOG_ERROR(Service_AM, "CIA file installation aborted with error code {:08x}",
result.Code().raw);
return InstallStatus::ErrorAborted;
}
total_bytes_read += bytes_read;
}
installFile.Close();
LOG_INFO(Service_AM, "Installed {} successfully.", path);
const FileUtil::DirectoryEntryCallable callback =
[&callback](u64* num_entries_out, const std::string& directory,
const std::string& virtual_name) -> bool {
const std::string physical_name = directory + DIR_SEP + virtual_name;
const bool is_dir = FileUtil::IsDirectory(physical_name);
if (!is_dir) {
std::unique_ptr<Loader::AppLoader> loader = Loader::GetLoader(physical_name);
if (!loader) {
return true;
}
bool executable = false;
const auto res = loader->IsExecutable(executable);
if (res == Loader::ResultStatus::ErrorEncrypted) {
return false;
}
return true;
} else {
return FileUtil::ForeachDirectoryEntry(nullptr, physical_name, callback);
}
};
if (!FileUtil::ForeachDirectoryEntry(
nullptr,
GetTitlePath(
Service::AM::GetTitleMediaType(container.GetTitleMetadata().GetTitleID()),
container.GetTitleMetadata().GetTitleID()),
callback)) {
LOG_ERROR(Service_AM, "CIA {} contained encrypted files.", path);
return InstallStatus::ErrorEncrypted;
}
return InstallStatus::Success;
}
LOG_ERROR(Service_AM, "CIA file {} is invalid!", path);
return InstallStatus::ErrorInvalid;
}
InstallStatus InstallFromNus(u64 title_id, int version) {
LOG_DEBUG(Service_AM, "Downloading {:X}", title_id);
CIAFile install_file{Core::System::GetInstance(), GetTitleMediaType(title_id)};
std::string path = fmt::format("/ccs/download/{:016X}/tmd", title_id);
if (version != -1) {
path += fmt::format(".{}", version);
}
auto tmd_response = Core::NUS::Download(path);
if (!tmd_response) {
LOG_ERROR(Service_AM, "Failed to download tmd for {:016X}", title_id);
return InstallStatus::ErrorFileNotFound;
}
FileSys::TitleMetadata tmd;
tmd.Load(*tmd_response);
path = fmt::format("/ccs/download/{:016X}/cetk", title_id);
auto cetk_response = Core::NUS::Download(path);
if (!cetk_response) {
LOG_ERROR(Service_AM, "Failed to download cetk for {:016X}", title_id);
return InstallStatus::ErrorFileNotFound;
}
std::vector<u8> content;
const auto content_count = tmd.GetContentCount();
for (std::size_t i = 0; i < content_count; ++i) {
const std::string filename = fmt::format("{:08x}", tmd.GetContentIDByIndex(i));
path = fmt::format("/ccs/download/{:016X}/{}", title_id, filename);
const auto temp_response = Core::NUS::Download(path);
if (!temp_response) {
LOG_ERROR(Service_AM, "Failed to download content for {:016X}", title_id);
return InstallStatus::ErrorFileNotFound;
}
content.insert(content.end(), temp_response->begin(), temp_response->end());
}
FileSys::CIAContainer::Header fake_header{
.header_size = sizeof(FileSys::CIAContainer::Header),
.type = 0,
.version = 0,
.cert_size = 0,
.tik_size = static_cast<u32_le>(cetk_response->size()),
.tmd_size = static_cast<u32_le>(tmd_response->size()),
.meta_size = 0,
};
for (u16 i = 0; i < content_count; ++i) {
fake_header.SetContentPresent(i);
}
std::vector<u8> header_data(sizeof(fake_header));
std::memcpy(header_data.data(), &fake_header, sizeof(fake_header));
std::size_t current_offset = 0;
const auto write_to_cia_file_aligned = [&install_file, ¤t_offset](std::vector<u8>& data) {
const u64 offset =
Common::AlignUp(current_offset + data.size(), FileSys::CIA_SECTION_ALIGNMENT);
data.resize(offset - current_offset, 0);
const auto result = install_file.Write(current_offset, data.size(), true, data.data());
if (result.Failed()) {
LOG_ERROR(Service_AM, "CIA file installation aborted with error code {:08x}",
result.Code().raw);
return InstallStatus::ErrorAborted;
}
current_offset += data.size();
return InstallStatus::Success;
};
auto result = write_to_cia_file_aligned(header_data);
if (result != InstallStatus::Success) {
return result;
}
result = write_to_cia_file_aligned(*cetk_response);
if (result != InstallStatus::Success) {
return result;
}
result = write_to_cia_file_aligned(*tmd_response);
if (result != InstallStatus::Success) {
return result;
}
result = write_to_cia_file_aligned(content);
if (result != InstallStatus::Success) {
return result;
}
return InstallStatus::Success;
}
u64 GetTitleUpdateId(u64 title_id) {
// Real services seem to just discard and replace the whole high word.
return (title_id & 0xFFFFFFFF) | (static_cast<u64>(TID_HIGH_UPDATE) << 32);
}
Service::FS::MediaType GetTitleMediaType(u64 titleId) {
u16 platform = static_cast<u16>(titleId >> 48);
u16 category = static_cast<u16>((titleId >> 32) & 0xFFFF);
u8 variation = static_cast<u8>(titleId & 0xFF);
if (platform != PLATFORM_CTR)
return Service::FS::MediaType::NAND;
if (category & CATEGORY_SYSTEM || category & CATEGORY_DLP || variation & VARIATION_SYSTEM)
return Service::FS::MediaType::NAND;
return Service::FS::MediaType::SDMC;
}
std::string GetTitleMetadataPath(Service::FS::MediaType media_type, u64 tid, bool update) {
std::string content_path = GetTitlePath(media_type, tid) + "content/";
if (media_type == Service::FS::MediaType::GameCard) {
LOG_ERROR(Service_AM, "Invalid request for nonexistent gamecard title metadata!");
return "";
}
// The TMD ID is usually held in the title databases, which we don't implement.
// For now, just scan for any .tmd files which exist, the smallest will be the
// base ID and the largest will be the (currently installing) update ID.
constexpr u32 MAX_TMD_ID = 0xFFFFFFFF;
u32 base_id = MAX_TMD_ID;
u32 update_id = 0;
FileUtil::FSTEntry entries;
FileUtil::ScanDirectoryTree(content_path, entries);
for (const FileUtil::FSTEntry& entry : entries.children) {
std::string filename_filename, filename_extension;
Common::SplitPath(entry.virtualName, nullptr, &filename_filename, &filename_extension);
if (filename_extension == ".tmd") {
const u32 id = static_cast<u32>(std::stoul(filename_filename, nullptr, 16));
base_id = std::min(base_id, id);
update_id = std::max(update_id, id);
}
}
// If we didn't find anything, default to 00000000.tmd for it to be created.
if (base_id == MAX_TMD_ID)
base_id = 0;
// Update ID should be one more than the last, if it hasn't been created yet.
if (base_id == update_id)
update_id++;
return content_path + fmt::format("{:08x}.tmd", (update ? update_id : base_id));
}
std::string GetTitleContentPath(Service::FS::MediaType media_type, u64 tid, std::size_t index,
bool update) {
if (media_type == Service::FS::MediaType::GameCard) {
// TODO(B3N30): check if TID matches
auto fs_user =
Core::System::GetInstance().ServiceManager().GetService<Service::FS::FS_USER>(
"fs:USER");
return fs_user->GetCurrentGamecardPath();
}
std::string content_path = GetTitlePath(media_type, tid) + "content/";
std::string tmd_path = GetTitleMetadataPath(media_type, tid, update);
u32 content_id = 0;
FileSys::TitleMetadata tmd;
if (tmd.Load(tmd_path) == Loader::ResultStatus::Success) {
if (index < tmd.GetContentCount()) {
content_id = tmd.GetContentIDByIndex(index);
} else {
LOG_ERROR(Service_AM, "Attempted to get path for non-existent content index {:04x}.",
index);
return "";
}
// TODO(shinyquagsire23): how does DLC actually get this folder on hardware?
// For now, check if the second (index 1) content has the optional flag set, for most
// apps this is usually the manual and not set optional, DLC has it set optional.
// All .apps (including index 0) will be in the 00000000/ folder for DLC.
if (tmd.GetContentCount() > 1 &&
tmd.GetContentTypeByIndex(1) & FileSys::TMDContentTypeFlag::Optional) {
content_path += "00000000/";
}
}
return fmt::format("{}{:08x}.app", content_path, content_id);
}
std::string GetTitlePath(Service::FS::MediaType media_type, u64 tid) {
// TODO(PabloMK7) TWL titles should be in TWL Nand. Assuming CTR Nand for now.
u32 high = static_cast<u32>(tid >> 32);
u32 low = static_cast<u32>(tid & 0xFFFFFFFF);
if (media_type == Service::FS::MediaType::NAND || media_type == Service::FS::MediaType::SDMC)
return fmt::format("{}{:08x}/{:08x}/", GetMediaTitlePath(media_type), high, low);
if (media_type == Service::FS::MediaType::GameCard) {
// TODO(B3N30): check if TID matches
auto fs_user =
Core::System::GetInstance().ServiceManager().GetService<Service::FS::FS_USER>(
"fs:USER");
return fs_user->GetCurrentGamecardPath();
}
return "";
}
std::string GetMediaTitlePath(Service::FS::MediaType media_type) {
if (media_type == Service::FS::MediaType::NAND)
return fmt::format("{}{}/title/", FileUtil::GetUserPath(FileUtil::UserPath::NANDDir),
SYSTEM_ID);
if (media_type == Service::FS::MediaType::SDMC)
return fmt::format("{}Nintendo 3DS/{}/{}/title/",
FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir), SYSTEM_ID,
SDCARD_ID);
if (media_type == Service::FS::MediaType::GameCard) {
// TODO(B3N30): check if TID matchess
auto fs_user =
Core::System::GetInstance().ServiceManager().GetService<Service::FS::FS_USER>(
"fs:USER");
return fs_user->GetCurrentGamecardPath();
}
return "";
}
void Module::ScanForTitles(Service::FS::MediaType media_type) {
am_title_list[static_cast<u32>(media_type)].clear();
std::string title_path = GetMediaTitlePath(media_type);
FileUtil::FSTEntry entries;
FileUtil::ScanDirectoryTree(title_path, entries, 1);
for (const FileUtil::FSTEntry& tid_high : entries.children) {
for (const FileUtil::FSTEntry& tid_low : tid_high.children) {
std::string tid_string = tid_high.virtualName + tid_low.virtualName;
if (tid_string.length() == TITLE_ID_VALID_LENGTH) {
const u64 tid = std::stoull(tid_string, nullptr, 16);
if (tid & TWL_TITLE_ID_FLAG) {
// TODO(PabloMK7) Move to TWL Nand, for now only check that
// the contents exists in CTR Nand as this is a SRL file
// instead of NCCH.
if (FileUtil::Exists(GetTitleContentPath(media_type, tid))) {
am_title_list[static_cast<u32>(media_type)].push_back(tid);
}
} else {
FileSys::NCCHContainer container(GetTitleContentPath(media_type, tid));
if (container.Load() == Loader::ResultStatus::Success) {
am_title_list[static_cast<u32>(media_type)].push_back(tid);
}
}
}
}
}
}
void Module::ScanForAllTitles() {
ScanForTitles(Service::FS::MediaType::NAND);
ScanForTitles(Service::FS::MediaType::SDMC);
}
Module::Interface::Interface(std::shared_ptr<Module> am, const char* name, u32 max_session)
: ServiceFramework(name, max_session), am(std::move(am)) {}
Module::Interface::~Interface() = default;
void Module::Interface::GetNumPrograms(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
u32 media_type = rp.Pop<u8>();
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess);
rb.Push<u32>(static_cast<u32>(am->am_title_list[media_type].size()));
}
void Module::Interface::FindDLCContentInfos(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>());
u64 title_id = rp.Pop<u64>();
u32 content_count = rp.Pop<u32>();
auto& content_requested_in = rp.PopMappedBuffer();
auto& content_info_out = rp.PopMappedBuffer();
// Validate that only DLC TIDs are passed in
u32 tid_high = static_cast<u32>(title_id >> 32);
if (tid_high != TID_HIGH_DLC) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 4);
rb.Push(Result(ErrCodes::InvalidTIDInList, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Usage));
rb.PushMappedBuffer(content_requested_in);
rb.PushMappedBuffer(content_info_out);
return;
}
std::vector<u16_le> content_requested(content_count);
content_requested_in.Read(content_requested.data(), 0, content_count * sizeof(u16));
std::string tmd_path = GetTitleMetadataPath(media_type, title_id);
FileSys::TitleMetadata tmd;
if (tmd.Load(tmd_path) == Loader::ResultStatus::Success) {
std::size_t write_offset = 0;
// Get info for each content index requested
for (std::size_t i = 0; i < content_count; i++) {
if (content_requested[i] >= tmd.GetContentCount()) {
LOG_ERROR(Service_AM,
"Attempted to get info for non-existent content index {:04x}.",
content_requested[i]);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 4);
rb.Push<u32>(-1); // TODO(Steveice10): Find the right error code
rb.PushMappedBuffer(content_requested_in);
rb.PushMappedBuffer(content_info_out);
return;
}
ContentInfo content_info = {};
content_info.index = content_requested[i];
content_info.type = tmd.GetContentTypeByIndex(content_requested[i]);
content_info.content_id = tmd.GetContentIDByIndex(content_requested[i]);
content_info.size = tmd.GetContentSizeByIndex(content_requested[i]);
content_info.ownership =
OWNERSHIP_OWNED; // TODO(Steveice10): Pull this from the ticket.
if (FileUtil::Exists(GetTitleContentPath(media_type, title_id, content_requested[i]))) {
content_info.ownership |= OWNERSHIP_DOWNLOADED;
}
content_info_out.Write(&content_info, write_offset, sizeof(ContentInfo));
write_offset += sizeof(ContentInfo);
}
}
IPC::RequestBuilder rb = rp.MakeBuilder(1, 4);
rb.Push(ResultSuccess);
rb.PushMappedBuffer(content_requested_in);
rb.PushMappedBuffer(content_info_out);
}
void Module::Interface::ListDLCContentInfos(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
u32 content_count = rp.Pop<u32>();
auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>());
u64 title_id = rp.Pop<u64>();
u32 start_index = rp.Pop<u32>();
auto& content_info_out = rp.PopMappedBuffer();
// Validate that only DLC TIDs are passed in
u32 tid_high = static_cast<u32>(title_id >> 32);
if (tid_high != TID_HIGH_DLC) {
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
rb.Push(Result(ErrCodes::InvalidTIDInList, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Usage));
rb.Push<u32>(0);
rb.PushMappedBuffer(content_info_out);
return;
}
std::string tmd_path = GetTitleMetadataPath(media_type, title_id);
u32 copied = 0;
FileSys::TitleMetadata tmd;
if (tmd.Load(tmd_path) == Loader::ResultStatus::Success) {
u32 end_index =
std::min(start_index + content_count, static_cast<u32>(tmd.GetContentCount()));
std::size_t write_offset = 0;
for (u32 i = start_index; i < end_index; i++) {
ContentInfo content_info = {};
content_info.index = static_cast<u16>(i);
content_info.type = tmd.GetContentTypeByIndex(i);
content_info.content_id = tmd.GetContentIDByIndex(i);
content_info.size = tmd.GetContentSizeByIndex(i);
content_info.ownership =
OWNERSHIP_OWNED; // TODO(Steveice10): Pull this from the ticket.
if (FileUtil::Exists(GetTitleContentPath(media_type, title_id, i))) {
content_info.ownership |= OWNERSHIP_DOWNLOADED;
}
content_info_out.Write(&content_info, write_offset, sizeof(ContentInfo));
write_offset += sizeof(ContentInfo);
copied++;
}
}
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
rb.Push(ResultSuccess);
rb.Push(copied);
rb.PushMappedBuffer(content_info_out);
}
void Module::Interface::DeleteContents(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
u8 media_type = rp.Pop<u8>();
u64 title_id = rp.Pop<u64>();
u32 content_count = rp.Pop<u32>();
auto& content_ids_in = rp.PopMappedBuffer();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(ResultSuccess);
rb.PushMappedBuffer(content_ids_in);
LOG_WARNING(Service_AM, "(STUBBED) media_type={}, title_id=0x{:016x}, content_count={}",
media_type, title_id, content_count);
}
void Module::Interface::GetProgramList(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
u32 count = rp.Pop<u32>();
u8 media_type = rp.Pop<u8>();
auto& title_ids_output = rp.PopMappedBuffer();
if (media_type > 2) {
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
rb.Push<u32>(-1); // TODO(shinyquagsire23): Find the right error code
rb.Push<u32>(0);
rb.PushMappedBuffer(title_ids_output);
return;
}
u32 media_count = static_cast<u32>(am->am_title_list[media_type].size());
u32 copied = std::min(media_count, count);
title_ids_output.Write(am->am_title_list[media_type].data(), 0, copied * sizeof(u64));
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
rb.Push(ResultSuccess);
rb.Push(copied);
rb.PushMappedBuffer(title_ids_output);
}
Result GetTitleInfoFromList(std::span<const u64> title_id_list, Service::FS::MediaType media_type,
Kernel::MappedBuffer& title_info_out) {
std::size_t write_offset = 0;
for (u32 i = 0; i < title_id_list.size(); i++) {
std::string tmd_path = GetTitleMetadataPath(media_type, title_id_list[i]);
TitleInfo title_info = {};
title_info.tid = title_id_list[i];
FileSys::TitleMetadata tmd;
if (tmd.Load(tmd_path) == Loader::ResultStatus::Success) {
// TODO(shinyquagsire23): This is the total size of all files this process owns,
// including savefiles and other content. This comes close but is off.
title_info.size = tmd.GetContentSizeByIndex(FileSys::TMDContentIndex::Main);
title_info.version = tmd.GetTitleVersion();
title_info.type = tmd.GetTitleType();
} else {
return Result(ErrorDescription::NotFound, ErrorModule::AM, ErrorSummary::InvalidState,
ErrorLevel::Permanent);
}
title_info_out.Write(&title_info, write_offset, sizeof(TitleInfo));
write_offset += sizeof(TitleInfo);
}
return ResultSuccess;
}
void Module::Interface::GetProgramInfos(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>());
u32 title_count = rp.Pop<u32>();
auto& title_id_list_buffer = rp.PopMappedBuffer();
auto& title_info_out = rp.PopMappedBuffer();
std::vector<u64> title_id_list(title_count);
title_id_list_buffer.Read(title_id_list.data(), 0, title_count * sizeof(u64));
Result result = GetTitleInfoFromList(title_id_list, media_type, title_info_out);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 4);
rb.Push(result);
rb.PushMappedBuffer(title_id_list_buffer);
rb.PushMappedBuffer(title_info_out);
}
void Module::Interface::GetProgramInfosIgnorePlatform(Kernel::HLERequestContext& ctx) {
GetProgramInfos(ctx);
}
void Module::Interface::DeleteUserProgram(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
auto media_type = rp.PopEnum<FS::MediaType>();
u64 title_id = rp.Pop<u64>();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
u16 category = static_cast<u16>((title_id >> 32) & 0xFFFF);
u8 variation = static_cast<u8>(title_id & 0xFF);
if (category & CATEGORY_SYSTEM || category & CATEGORY_DLP || variation & VARIATION_SYSTEM) {
LOG_ERROR(Service_AM, "Trying to uninstall system app");
rb.Push(Result(ErrCodes::TryingToUninstallSystemApp, ErrorModule::AM,
ErrorSummary::InvalidArgument, ErrorLevel::Usage));
return;
}
LOG_INFO(Service_AM, "Deleting title 0x{:016x}", title_id);
std::string path = GetTitlePath(media_type, title_id);
if (!FileUtil::Exists(path)) {
rb.Push(Result(ErrorDescription::NotFound, ErrorModule::AM, ErrorSummary::InvalidState,
ErrorLevel::Permanent));
LOG_ERROR(Service_AM, "Title not found");
return;
}
bool success = FileUtil::DeleteDirRecursively(path);
am->ScanForAllTitles();
rb.Push(ResultSuccess);
if (!success)
LOG_ERROR(Service_AM, "FileUtil::DeleteDirRecursively unexpectedly failed");
}
void Module::Interface::GetProductCode(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
FS::MediaType media_type = rp.PopEnum<FS::MediaType>();
u64 title_id = rp.Pop<u64>();
std::string path = GetTitleContentPath(media_type, title_id);
if (!FileUtil::Exists(path)) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(Result(ErrorDescription::NotFound, ErrorModule::AM, ErrorSummary::InvalidState,
ErrorLevel::Permanent));
} else {
struct ProductCode {
u8 code[0x10];
};
ProductCode product_code;
IPC::RequestBuilder rb = rp.MakeBuilder(6, 0);
FileSys::NCCHContainer ncch(path);
ncch.Load();
std::memcpy(&product_code.code, &ncch.ncch_header.product_code, 0x10);
rb.Push(ResultSuccess);
rb.PushRaw(product_code);
}
}
void Module::Interface::GetDLCTitleInfos(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>());
u32 title_count = rp.Pop<u32>();
auto& title_id_list_buffer = rp.PopMappedBuffer();
auto& title_info_out = rp.PopMappedBuffer();
std::vector<u64> title_id_list(title_count);
title_id_list_buffer.Read(title_id_list.data(), 0, title_count * sizeof(u64));
Result result = ResultSuccess;
// Validate that DLC TIDs were passed in
for (u32 i = 0; i < title_count; i++) {
u32 tid_high = static_cast<u32>(title_id_list[i] >> 32);
if (tid_high != TID_HIGH_DLC) {
result = Result(ErrCodes::InvalidTIDInList, ErrorModule::AM,
ErrorSummary::InvalidArgument, ErrorLevel::Usage);
break;
}
}
if (result.IsSuccess()) {
result = GetTitleInfoFromList(title_id_list, media_type, title_info_out);
}
IPC::RequestBuilder rb = rp.MakeBuilder(1, 4);
rb.Push(result);
rb.PushMappedBuffer(title_id_list_buffer);
rb.PushMappedBuffer(title_info_out);
}
void Module::Interface::GetPatchTitleInfos(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>());
u32 title_count = rp.Pop<u32>();
auto& title_id_list_buffer = rp.PopMappedBuffer();
auto& title_info_out = rp.PopMappedBuffer();
std::vector<u64> title_id_list(title_count);
title_id_list_buffer.Read(title_id_list.data(), 0, title_count * sizeof(u64));
Result result = ResultSuccess;
// Validate that update TIDs were passed in
for (u32 i = 0; i < title_count; i++) {
u32 tid_high = static_cast<u32>(title_id_list[i] >> 32);
if (tid_high != TID_HIGH_UPDATE) {
result = Result(ErrCodes::InvalidTIDInList, ErrorModule::AM,
ErrorSummary::InvalidArgument, ErrorLevel::Usage);
break;
}
}
if (result.IsSuccess()) {
result = GetTitleInfoFromList(title_id_list, media_type, title_info_out);
}
IPC::RequestBuilder rb = rp.MakeBuilder(1, 4);
rb.Push(result);
rb.PushMappedBuffer(title_id_list_buffer);
rb.PushMappedBuffer(title_info_out);
}
void Module::Interface::ListDataTitleTicketInfos(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
u32 ticket_count = rp.Pop<u32>();
u64 title_id = rp.Pop<u64>();
u32 start_index = rp.Pop<u32>();
auto& ticket_info_out = rp.PopMappedBuffer();
std::size_t write_offset = 0;
for (u32 i = 0; i < ticket_count; i++) {
TicketInfo ticket_info = {};
ticket_info.title_id = title_id;
ticket_info.version = 0; // TODO
ticket_info.size = 0; // TODO
ticket_info_out.Write(&ticket_info, write_offset, sizeof(TicketInfo));
write_offset += sizeof(TicketInfo);
}
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
rb.Push(ResultSuccess);
rb.Push(ticket_count);
rb.PushMappedBuffer(ticket_info_out);
LOG_WARNING(Service_AM,
"(STUBBED) ticket_count=0x{:08X}, title_id=0x{:016x}, start_index=0x{:08X}",
ticket_count, title_id, start_index);
}
void Module::Interface::GetDLCContentInfoCount(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>());
u64 title_id = rp.Pop<u64>();
// Validate that only DLC TIDs are passed in
u32 tid_high = static_cast<u32>(title_id >> 32);
if (tid_high != TID_HIGH_DLC) {
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
rb.Push(Result(ErrCodes::InvalidTID, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Usage));
rb.Push<u32>(0);
return;
}
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess); // No error
std::string tmd_path = GetTitleMetadataPath(media_type, title_id);
FileSys::TitleMetadata tmd;
if (tmd.Load(tmd_path) == Loader::ResultStatus::Success) {
rb.Push<u32>(static_cast<u32>(tmd.GetContentCount()));
} else {
rb.Push<u32>(1); // Number of content infos plus one
LOG_WARNING(Service_AM, "(STUBBED) called media_type={}, title_id=0x{:016x}", media_type,
title_id);
}
}
void Module::Interface::DeleteTicket(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
u64 title_id = rp.Pop<u64>();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultSuccess);
LOG_WARNING(Service_AM, "(STUBBED) called title_id=0x{:016x}", title_id);
}
void Module::Interface::GetNumTickets(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
u32 ticket_count = 0;
for (const auto& title_list : am->am_title_list) {
ticket_count += static_cast<u32>(title_list.size());
}
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess);
rb.Push(ticket_count);
LOG_WARNING(Service_AM, "(STUBBED) called ticket_count=0x{:08x}", ticket_count);
}
void Module::Interface::GetTicketList(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
u32 ticket_list_count = rp.Pop<u32>();
u32 ticket_index = rp.Pop<u32>();
auto& ticket_tids_out = rp.PopMappedBuffer();
u32 tickets_written = 0;
for (const auto& title_list : am->am_title_list) {
const auto tickets_to_write =
std::min(static_cast<u32>(title_list.size()), ticket_list_count - tickets_written);
ticket_tids_out.Write(title_list.data(), tickets_written * sizeof(u64),
tickets_to_write * sizeof(u64));
tickets_written += tickets_to_write;
}
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
rb.Push(ResultSuccess);
rb.Push(tickets_written);
rb.PushMappedBuffer(ticket_tids_out);
LOG_WARNING(Service_AM, "(STUBBED) ticket_list_count=0x{:08x}, ticket_index=0x{:08x}",
ticket_list_count, ticket_index);
}
void Module::Interface::NeedsCleanup(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
const auto media_type = rp.Pop<u8>();
LOG_DEBUG(Service_AM, "(STUBBED) media_type=0x{:02x}", media_type);
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess);
rb.Push<bool>(false);
}
void Module::Interface::DoCleanup(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
const auto media_type = rp.Pop<u8>();
LOG_DEBUG(Service_AM, "(STUBBED) called, media_type={:#02x}", media_type);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultSuccess);
}
void Module::Interface::QueryAvailableTitleDatabase(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
u8 media_type = rp.Pop<u8>();
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess); // No error
rb.Push(true);
LOG_WARNING(Service_AM, "(STUBBED) media_type={}", media_type);
}
void Module::Interface::GetPersonalizedTicketInfoList(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
[[maybe_unused]] u32 ticket_count = rp.Pop<u32>();
[[maybe_unused]] auto& buffer = rp.PopMappedBuffer();
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess); // No error
rb.Push(0);
LOG_WARNING(Service_AM, "(STUBBED) called, ticket_count={}", ticket_count);
}
void Module::Interface::GetNumImportTitleContextsFiltered(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
u8 media_type = rp.Pop<u8>();
u8 filter = rp.Pop<u8>();
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess); // No error
rb.Push(0);
LOG_WARNING(Service_AM, "(STUBBED) called, media_type={}, filter={}", media_type, filter);
}
void Module::Interface::GetImportTitleContextListFiltered(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
[[maybe_unused]] const u32 count = rp.Pop<u32>();
const u8 media_type = rp.Pop<u8>();
const u8 filter = rp.Pop<u8>();
auto& buffer = rp.PopMappedBuffer();
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
rb.Push(ResultSuccess); // No error
rb.Push(0);
rb.PushMappedBuffer(buffer);
LOG_WARNING(Service_AM, "(STUBBED) called, media_type={}, filter={}", media_type, filter);
}
void Module::Interface::CheckContentRights(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
u64 tid = rp.Pop<u64>();
u16 content_index = rp.Pop<u16>();
// TODO(shinyquagsire23): Read tickets for this instead?
bool has_rights =
FileUtil::Exists(GetTitleContentPath(Service::FS::MediaType::NAND, tid, content_index)) ||
FileUtil::Exists(GetTitleContentPath(Service::FS::MediaType::SDMC, tid, content_index));
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess); // No error
rb.Push(has_rights);
LOG_WARNING(Service_AM, "(STUBBED) tid={:016x}, content_index={}", tid, content_index);
}
void Module::Interface::CheckContentRightsIgnorePlatform(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
u64 tid = rp.Pop<u64>();
u16 content_index = rp.Pop<u16>();
// TODO(shinyquagsire23): Read tickets for this instead?
bool has_rights =
FileUtil::Exists(GetTitleContentPath(Service::FS::MediaType::SDMC, tid, content_index));
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess); // No error
rb.Push(has_rights);
LOG_WARNING(Service_AM, "(STUBBED) tid={:016x}, content_index={}", tid, content_index);
}
void Module::Interface::BeginImportProgram(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>());
if (am->cia_installing) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(Result(ErrCodes::CIACurrentlyInstalling, ErrorModule::AM,
ErrorSummary::InvalidState, ErrorLevel::Permanent));
return;
}
// Create our CIAFile handle for the app to write to, and while the app writes
// Citra will store contents out to sdmc/nand
const FileSys::Path cia_path = {};
auto file = std::make_shared<Service::FS::File>(
am->system.Kernel(), std::make_unique<CIAFile>(am->system, media_type), cia_path);
am->cia_installing = true;
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(ResultSuccess); // No error
rb.PushCopyObjects(file->Connect());
LOG_WARNING(Service_AM, "(STUBBED) media_type={}", media_type);
}
void Module::Interface::BeginImportProgramTemporarily(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
if (am->cia_installing) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(Result(ErrCodes::CIACurrentlyInstalling, ErrorModule::AM,
ErrorSummary::InvalidState, ErrorLevel::Permanent));
return;
}
// Note: This function should register the title in the temp_i.db database, but we can get away
// with not doing that because we traverse the file system to detect installed titles.
// Create our CIAFile handle for the app to write to, and while the app writes Citra will store
// contents out to sdmc/nand
const FileSys::Path cia_path = {};
auto file = std::make_shared<Service::FS::File>(
am->system.Kernel(), std::make_unique<CIAFile>(am->system, FS::MediaType::NAND), cia_path);
am->cia_installing = true;
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(ResultSuccess); // No error
rb.PushCopyObjects(file->Connect());
LOG_WARNING(Service_AM, "(STUBBED)");
}
void Module::Interface::EndImportProgram(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
[[maybe_unused]] const auto cia = rp.PopObject<Kernel::ClientSession>();
am->ScanForAllTitles();
am->cia_installing = false;
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultSuccess);
}
void Module::Interface::EndImportProgramWithoutCommit(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
[[maybe_unused]] const auto cia = rp.PopObject<Kernel::ClientSession>();
// Note: This function is basically a no-op for us since we don't use title.db or ticket.db
// files to keep track of installed titles.
am->ScanForAllTitles();
am->cia_installing = false;
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultSuccess);
}
void Module::Interface::CommitImportPrograms(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
[[maybe_unused]] const auto media_type = static_cast<FS::MediaType>(rp.Pop<u8>());
[[maybe_unused]] const u32 title_count = rp.Pop<u32>();
[[maybe_unused]] const u8 database = rp.Pop<u8>();
const auto buffer = rp.PopMappedBuffer();
// Note: This function is basically a no-op for us since we don't use title.db or ticket.db
// files to keep track of installed titles.
am->ScanForAllTitles();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(ResultSuccess);
rb.PushMappedBuffer(buffer);
}
/// Wraps all File operations to allow adding an offset to them.
class AMFileWrapper : public FileSys::FileBackend {
public:
AMFileWrapper(std::shared_ptr<Service::FS::File> file, std::size_t offset, std::size_t size)
: file(std::move(file)), file_offset(offset), file_size(size) {}
ResultVal<std::size_t> Read(u64 offset, std::size_t length, u8* buffer) const override {
return file->backend->Read(offset + file_offset, length, buffer);
}
ResultVal<std::size_t> Write(u64 offset, std::size_t length, bool flush,
const u8* buffer) override {
return file->backend->Write(offset + file_offset, length, flush, buffer);
}
u64 GetSize() const override {
return file_size;
}
bool SetSize(u64 size) const override {
return false;
}
bool Close() const override {
return false;
}
void Flush() const override {}
private:
std::shared_ptr<Service::FS::File> file;
std::size_t file_offset;
std::size_t file_size;
};
ResultVal<std::unique_ptr<AMFileWrapper>> GetFileFromSession(
std::shared_ptr<Kernel::ClientSession> file_session) {
// Step up the chain from ClientSession->ServerSession and then
// cast to File. For AM on 3DS, invalid handles actually hang the system.
if (file_session->parent == nullptr) {
LOG_WARNING(Service_AM, "Invalid file handle!");
return Kernel::ResultInvalidHandle;
}
std::shared_ptr<Kernel::ServerSession> server =
Kernel::SharedFrom(file_session->parent->server);
if (server == nullptr) {
LOG_WARNING(Service_AM, "File handle ServerSession disconnected!");
return Kernel::ResultSessionClosed;
}
if (server->hle_handler != nullptr) {
auto file = std::dynamic_pointer_cast<Service::FS::File>(server->hle_handler);
// TODO(shinyquagsire23): This requires RTTI, use service calls directly instead?
if (file != nullptr) {
// Grab the session file offset in case we were given a subfile opened with
// File::OpenSubFile
std::size_t offset = file->GetSessionFileOffset(server);
std::size_t size = file->GetSessionFileSize(server);
return std::make_unique<AMFileWrapper>(file, offset, size);
}
LOG_ERROR(Service_AM, "Failed to cast handle to FSFile!");
return Kernel::ResultInvalidHandle;
}
// Probably the best bet if someone is LLEing the fs service is to just have them LLE AM
// while they're at it, so not implemented.
LOG_ERROR(Service_AM, "Given file handle does not have an HLE handler!");
return Kernel::ResultNotImplemented;
}
void Module::Interface::GetProgramInfoFromCia(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
[[maybe_unused]] const auto media_type = static_cast<FS::MediaType>(rp.Pop<u8>());
auto cia = rp.PopObject<Kernel::ClientSession>();
auto file_res = GetFileFromSession(cia);
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(file_res.Code());
return;
}
FileSys::CIAContainer container;
if (container.Load(*file_res.Unwrap()) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(Result(ErrCodes::InvalidCIAHeader, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Permanent));
return;
}
const FileSys::TitleMetadata& tmd = container.GetTitleMetadata();
TitleInfo title_info = {};
container.Print();
// TODO(shinyquagsire23): Sizes allegedly depend on the mediatype, and will double
// on some mediatypes. Since this is more of a required install size we'll report
// what Citra needs, but it would be good to be more accurate here.
title_info.tid = tmd.GetTitleID();
title_info.size = tmd.GetContentSizeByIndex(FileSys::TMDContentIndex::Main);
title_info.version = tmd.GetTitleVersion();
title_info.type = tmd.GetTitleType();
IPC::RequestBuilder rb = rp.MakeBuilder(8, 0);
rb.Push(ResultSuccess);
rb.PushRaw<TitleInfo>(title_info);
}
void Module::Interface::GetSystemMenuDataFromCia(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
auto cia = rp.PopObject<Kernel::ClientSession>();
auto& output_buffer = rp.PopMappedBuffer();
auto file_res = GetFileFromSession(cia);
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(file_res.Code());
rb.PushMappedBuffer(output_buffer);
return;
}
std::size_t output_buffer_size = std::min(output_buffer.GetSize(), sizeof(Loader::SMDH));
auto file = std::move(file_res.Unwrap());
FileSys::CIAContainer container;
if (container.Load(*file) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(Result(ErrCodes::InvalidCIAHeader, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Permanent));
rb.PushMappedBuffer(output_buffer);
return;
}
std::vector<u8> temp(output_buffer_size);
// Read from the Meta offset + 0x400 for the 0x36C0-large SMDH
auto read_result = file->Read(container.GetMetadataOffset() + FileSys::CIA_METADATA_SIZE,
temp.size(), temp.data());
if (read_result.Failed() || *read_result != temp.size()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(Result(ErrCodes::InvalidCIAHeader, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Permanent));
rb.PushMappedBuffer(output_buffer);
return;
}
output_buffer.Write(temp.data(), 0, temp.size());
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(ResultSuccess);
rb.PushMappedBuffer(output_buffer);
}
void Module::Interface::GetDependencyListFromCia(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
auto cia = rp.PopObject<Kernel::ClientSession>();
auto file_res = GetFileFromSession(cia);
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(file_res.Code());
return;
}
FileSys::CIAContainer container;
if (container.Load(*file_res.Unwrap()) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(Result(ErrCodes::InvalidCIAHeader, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Permanent));
return;
}
std::vector<u8> buffer(FileSys::CIA_DEPENDENCY_SIZE);
std::memcpy(buffer.data(), container.GetDependencies().data(), buffer.size());
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(ResultSuccess);
rb.PushStaticBuffer(std::move(buffer), 0);
}
void Module::Interface::GetTransferSizeFromCia(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
auto cia = rp.PopObject<Kernel::ClientSession>();
auto file_res = GetFileFromSession(cia);
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(file_res.Code());
return;
}
FileSys::CIAContainer container;
if (container.Load(*file_res.Unwrap()) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(Result(ErrCodes::InvalidCIAHeader, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Permanent));
return;
}
IPC::RequestBuilder rb = rp.MakeBuilder(3, 0);
rb.Push(ResultSuccess);
rb.Push(container.GetMetadataOffset());
}
void Module::Interface::GetCoreVersionFromCia(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
auto cia = rp.PopObject<Kernel::ClientSession>();
auto file_res = GetFileFromSession(cia);
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(file_res.Code());
return;
}
FileSys::CIAContainer container;
if (container.Load(*file_res.Unwrap()) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(Result(ErrCodes::InvalidCIAHeader, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Permanent));
return;
}
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess);
rb.Push(container.GetCoreVersion());
}
void Module::Interface::GetRequiredSizeFromCia(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
[[maybe_unused]] const auto media_type = static_cast<FS::MediaType>(rp.Pop<u8>());
auto cia = rp.PopObject<Kernel::ClientSession>();
auto file_res = GetFileFromSession(cia);
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(file_res.Code());
return;
}
FileSys::CIAContainer container;
if (container.Load(*file_res.Unwrap()) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(Result(ErrCodes::InvalidCIAHeader, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Permanent));
return;
}
// TODO(shinyquagsire23): Sizes allegedly depend on the mediatype, and will double
// on some mediatypes. Since this is more of a required install size we'll report
// what Citra needs, but it would be good to be more accurate here.
IPC::RequestBuilder rb = rp.MakeBuilder(3, 0);
rb.Push(ResultSuccess);
rb.Push(container.GetTitleMetadata().GetContentSizeByIndex(FileSys::TMDContentIndex::Main));
}
Result UninstallProgram(const FS::MediaType media_type, const u64 title_id) {
// Use the content folder so we don't delete the user's save data.
const auto path = GetTitlePath(media_type, title_id) + "content/";
if (!FileUtil::Exists(path)) {
return {ErrorDescription::NotFound, ErrorModule::AM, ErrorSummary::InvalidState,
ErrorLevel::Permanent};
}
if (!FileUtil::DeleteDirRecursively(path)) {
// TODO: Determine the right error code for this.
return {ErrorDescription::NotFound, ErrorModule::AM, ErrorSummary::InvalidState,
ErrorLevel::Permanent};
}
return ResultSuccess;
}
void Module::Interface::DeleteProgram(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
const auto media_type = rp.PopEnum<FS::MediaType>();
const auto title_id = rp.Pop<u64>();
LOG_INFO(Service_AM, "called, title={:016x}", title_id);
const auto result = UninstallProgram(media_type, title_id);
am->ScanForAllTitles();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(result);
}
void Module::Interface::GetSystemUpdaterMutex(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(ResultSuccess);
rb.PushCopyObjects(am->system_updater_mutex);
}
void Module::Interface::GetMetaSizeFromCia(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
auto cia = rp.PopObject<Kernel::ClientSession>();
auto file_res = GetFileFromSession(cia);
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(file_res.Code());
return;
}
FileSys::CIAContainer container;
if (container.Load(*file_res.Unwrap()) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(Result(ErrCodes::InvalidCIAHeader, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Permanent));
return;
}
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess);
rb.Push(container.GetMetadataSize());
}
void Module::Interface::GetMetaDataFromCia(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
u32 output_size = rp.Pop<u32>();
auto cia = rp.PopObject<Kernel::ClientSession>();
auto& output_buffer = rp.PopMappedBuffer();
auto file_res = GetFileFromSession(cia);
if (!file_res.Succeeded()) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(file_res.Code());
rb.PushMappedBuffer(output_buffer);
return;
}
// Don't write beyond the actual static buffer size.
output_size = std::min(static_cast<u32>(output_buffer.GetSize()), output_size);
auto file = std::move(file_res.Unwrap());
FileSys::CIAContainer container;
if (container.Load(*file) != Loader::ResultStatus::Success) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(Result(ErrCodes::InvalidCIAHeader, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Permanent));
rb.PushMappedBuffer(output_buffer);
return;
}
// Read from the Meta offset for the specified size
std::vector<u8> temp(output_size);
auto read_result = file->Read(container.GetMetadataOffset(), output_size, temp.data());
if (read_result.Failed() || *read_result != output_size) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(Result(ErrCodes::InvalidCIAHeader, ErrorModule::AM, ErrorSummary::InvalidArgument,
ErrorLevel::Permanent));
return;
}
output_buffer.Write(temp.data(), 0, output_size);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(ResultSuccess);
rb.PushMappedBuffer(output_buffer);
}
void Module::Interface::BeginImportTicket(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
// Create our TicketFile handle for the app to write to
auto file = std::make_shared<Service::FS::File>(
am->system.Kernel(), std::make_unique<TicketFile>(), FileSys::Path{});
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(ResultSuccess); // No error
rb.PushCopyObjects(file->Connect());
LOG_WARNING(Service_AM, "(STUBBED) called");
}
void Module::Interface::EndImportTicket(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
[[maybe_unused]] const auto ticket = rp.PopObject<Kernel::ClientSession>();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultSuccess);
LOG_WARNING(Service_AM, "(STUBBED) called");
}
template <class Archive>
void Module::serialize(Archive& ar, const unsigned int) {
ar& cia_installing;
ar& am_title_list;
ar& system_updater_mutex;
}
SERIALIZE_IMPL(Module)
Module::Module(Core::System& system) : system(system) {
ScanForAllTitles();
system_updater_mutex = system.Kernel().CreateMutex(false, "AM::SystemUpdaterMutex");
}
Module::~Module() = default;
void InstallInterfaces(Core::System& system) {
auto& service_manager = system.ServiceManager();
auto am = std::make_shared<Module>(system);
std::make_shared<AM_APP>(am)->InstallAsService(service_manager);
std::make_shared<AM_NET>(am)->InstallAsService(service_manager);
std::make_shared<AM_SYS>(am)->InstallAsService(service_manager);
std::make_shared<AM_U>(am)->InstallAsService(service_manager);
}
} // namespace Service::AM
|